for loop through a list of data frames and save results in new data frame
r
Solution
One way is to use `lapply` with `rbind` and then call `as.data.frame`. Since there is no data to play with, I'll give the general idea.
o <- lapply(1:length(covpatient), function(i) {
a <- names(covpatient[i])
b <- sum(covpatient[[i]]$cov == 0)
c <- sum(covpatient[[i]]$cov > 0 & covpatient[[i]]$cov <= 40)
d <- sum(covpatient[[i]]$cov > 40 & covpatient[[i]]$cov <= 100)
e <- sum(covpatient[[i]]$cov > 100)
c(a,b,c,d,e)
})
out <- as.data.frame(do.call(rbind, o))
Problem
I want to fetch some values out of a list of data frames and save the results in a new data frame. This is how my code looks: ``` for (i in 1:length(covpatient)){ a <- names(covpatient[i]) b <- sum(covpatient[[i]]$cov == 0) c <- sum(covpatient[[i]]$cov > 0 & covpatient[[i]]$cov <= 40) d <- sum(covpatient[[i]]$cov > 40 & covpatient[[i]]$cov <= 100) e <- sum(covpatient[[i]]$cov > 100) summary <- c(a,b,c,d,e) ``` } So for every dataframe in the list covpatient, I want to create a summary variable which consists of 5 elements (a,b,c,d,e). Then I want to make a new data.frame which stores all summary values (5 columns & i rows). Could someone give me a hand?