I would like to create a loop that gets the summary split on factor levels for each variable. For example, if I wanted to get summary split by factor levels within "grouping" variable, I would use:
df %>%
select(grouping, length, weight) %>%
split(.$grouping) %>%
map(summary)
However, I am not sure how to put this into a loop, such that I get a summary based on the factor levels of each of the variables of interest within my dataframe.
For example, I can get the summary() for variables in columns 3 and 4 of dataframe using:
# Dummy data
length = sample(30:60, 10, replace = FALSE)
weight = sample(50:70, 10, replace = FALSE)
grouping = c("A", "A", "B", "A", "B", "A", "B", "B", "B", "A")
colour = c("Blue", "Green", "Green", "Green", "Blue", "Blue", "Blue", "Green", "Blue", "Green")
type = c("Case", "Control", "Case", "Case", "Case", "Control", "Control", "Case", "Control", "Case")
df = data.frame(length, weight, grouping, colour, type)
# Variables to loop
colNames <- names(df)[c(3:4)]
# Summary
for(i in colNames){
# Summary
summary <- df %>%
select(length, weight, .$colNames[i]) %>%
summary()
print(summary)
}
But I can't do it when split by factor levels for each variable:
# Variables to loop
colNames = names(df)[c(3,4)]
# Summary
for(i in colNames){
df %>%
select(length, weight, .$colNames[i]) %>%
split(.$colNames[i]) %>%
summary()
}
I figure split(.colNames) is the problem, but I am not sure how to fix it. Thank you for any help!