I have a dataframe df, which has variables component1_done, component2_done and so on. I want to table each, for example, as:
proportions(table(df$component1_done))
proportions(table(df$component2_done))
proportions(table(df$component3_done))
proportions(table(df$component4_done))
proportions(table(df$component5_done))
proportions(table(df$component6_done))
How can I do the same using a loop? In other words, how can I refer to df$component"i"_done in a loop?
Thanks!
CodePudding user response:
You can use lapply
for that. The result is a list of proportions for all columns.
df <- data.frame(a=c(1,2,2,4,5), b=c(3,4,5,6,6))
lapply(df, function(x) proportions(table(x)))
$a
x
1 2 4 5
0.2 0.4 0.2 0.2
$b
x
3 4 5 6
0.2 0.2 0.2 0.4
With dplyr
library(dplyr)
as.list(df %>% summarise(across(everything(), ~ proportions(table(.x)))))
$a
a
1 2 4 5
0.2 0.4 0.2 0.2
$b
b
3 4 5 6
0.2 0.2 0.2 0.4
CodePudding user response:
Thank you so much, Andre! Yours is of course much prettier, but in the meanwhile I have also struggled to use a loop. Our answer was to use a longer format.
for (i in 1:6){
df0 <- df %>% filter(answerindex==8)
char <- paste0("component",i,"_done")
df0 <- df0 %>% select("username",all_of(char))
df0_longer <- pivot_longer(df0, col = -c(username), names_to = "component", values_to = "done")
t <- proportions(table(df0_longer$done))
print(t)
}
This works but I was very unhappy that this was longer than the original six lines ;)
CodePudding user response:
If you really want to use a loop you can use
df <- data.frame(a=c(1,2,2,4,5), b=c(3,4,5,6,6))
for (i in seq_along(names(df))){
print(proportions(table(df[,i])))
}
Loops don't print unless you say to.
Alternatively you can assign each table to a list and then print the list.
tables <- list()
for (i in seq_along(names(df))){
tables[[i]] <- proportions(table(df[,i]))
}
tables