I have a list containing two data frames:
sample_list <- list("tables" = data.frame(weight = sample(1:50, 20, replace = T)),
"chairs" = data.frame(height = sample(1:50, 20, replace = T)))
I would like to use lapply to run a function over all the data frames in this list. In the output of each function, I need to create another column with the name of the source data frame (see mutate):
lapply(sample_list, function(x) {
x %>%
filter(x >= 20) %>%
mutate(groupName = names(x))
})
For some reason, I can't figure out how to make this work. How do I pass the name of the data frame into mutate? Right now it is returning the name of the first column in that data frame, rather than the name of the data frame itself.
Thanks!