I am trying to save the results of all interactions, but for loop
only gives me the result of last interaction. Just like this:
l <- list(a = c(1, 3, 5), b = c(4, 8), c = 2)
df <- data.frame()
for (i in 1:length(l)) {
s <- data.frame(name = names(l[i]),
value = mean(l[[i]]))
out <- rbind(df, s)
}
This code returns this:
I need to something like this:
How can I solve this?
Thanks in advance!
CodePudding user response:
Your out
variable only contains the result of the last iteration since out
is overriden in every iteration of the loop.
Replace out
by df
like so, your expected result will be in the df
variable:
l <- list(a = c(1, 3, 5), b = c(4, 8), c = 2)
df <- data.frame()
for (i in 1:length(l)) {
s <- data.frame(name = names(l[i]),
value = mean(l[[i]]))
df <- rbind(df, s)
}
df