I am trying to count the occurrences of some observations according to the value taken by the 'unimodal_preset' variable (which you might find in the dataset I let you attached below) and store them in a small table, built as follows:
tab <- matrix(rep(0, times= 16), ncol= 2, byrow=TRUE)
colnames(tab) <- c('congruent', 'incongruent')
rownames(tab) <- rep(c('written', 'spoken'), 4)
tab <- as.table(tab)
tab
I have prepared the following for loop:
for (i in rownames(tab)){
for (j in colnames(tab)){
if (di$unimodal_preset == 0 & di$cong_cond == 'congreunt' & di$presentation_mode == 'spoken'){
tab[i,j] <- di %>% filter(cong_cond == 'congruent' & presentation_mode == 'spoken' & unimodal_preset == 0) %>% summarise(n = n()) %>% as.data.frame()
} else if (di$unimodal_preset == 0 & di$cong_cond == 'congreunt' & di$presentation_mode == 'written') {
tab[i,j] <- di %>% filter(cong_cond == 'congruent' & presentation_mode == 'written' & unimodal_preset == 0) %>% summarise(n = n()) %>% as.data.frame()
} else if (di$unimodal_preset == 0 & di$cong_cond == 'incongreunt' & di$presentation_mode == 'spoken') {
tab[i,j] <- di %>% filter(cong_cond == 'incongruent' & presentation_mode == 'spoken' & unimodal_preset == 0) %>% summarise(n = n()) %>% as.data.frame()
} else if (di$unimodal_preset == 0 & di$cong_cond == 'incongreunt' & di$presentation_mode == 'written') {
tab[i,j] <- di %>% filter(cong_cond == 'incongruent' & presentation_mode == 'written' & unimodal_preset == 0) %>% summarise(n = n()) %>% as.data.frame()
} else if (di$unimodal_preset == 1 & di$cong_cond == 'congreunt' & di$presentation_mode == 'spoken') {
tab[i,j] <- di %>% filter(cong_cond == 'congruent' & presentation_mode == 'spoken' & unimodal_preset == 1) %>% summarise(n = n()) %>% as.data.frame()
} else if (di$unimodal_preset == 1 & di$cong_cond == 'congreunt' & di$presentation_mode == 'written') {
tab[i,j] <- di %>% filter(cong_cond == 'congruent' & presentation_mode == 'written' & unimodal_preset == 1) %>% summarise(n = n()) %>% as.data.frame()
} else if (di$unimodal_preset == 1 & di$cong_cond == 'incongreunt' & di$presentation_mode == 'spoken') {
tab[i,j] <- di %>% filter(cong_cond == 'incongruent' & presentation_mode == 'spoken' & unimodal_preset == 1) %>% summarise(n = n()) %>% as.data.frame()
}
else if (di$unimodal == 1 & di$cong_cond == 'incongreunt' & di$presentation_mode == 'written'){
tab[i,j] <- di %>% filter(cong_cond == 'incongruent' & presentation_mode == 'written' & unimodal_preset == 1) %>% summarise(n = n())%>% as.data.frame()
}
}
}
print(tab)
but unfortunately, it does not seem to work on my end. Could you please suggest a possible way through? Here is the dataset I have used
> dput(head(di, 150))
structure(list(cong_cond = c("congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"incongruent", "incongruent", "incongruent", "incongruent", "incongruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent", "congruent", "congruent", "congruent",
"congruent", "congruent"), presentation_mode = c("spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "written", "written", "written", "written", "written",
"written", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "spoken", "spoken", "spoken", "spoken",
"spoken", "spoken", "spoken", "written", "written", "written"
), unimodal_preset = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -150L), class = c("tbl_df",
"tbl", "data.frame"))