If we need to update the original data.frame objects with the new value, then use assign
nm1 <- paste0("df", 1:3)
for(i in seq_along(L)) {
assign(nm1[i], `[<-`(L[[i]], "Average", value = mean(L[[i]]$Number)))
}
df1
# Number Average
#1 45 42.85714
#2 62 42.85714
#3 27 42.85714
#4 34 42.85714
#5 37 42.85714
#6 55 42.85714
#7 40 42.85714
Regarding why the OP's loop didn't work,
for(i in L) print(i)
returns the value of the list and not the names of the objects. So, we cannot an assignment i$Average <-. The list elements don't have names. Also, mean works on a vector. It can be directly applied on data.frame
mean(L[[1]])
#[1] NA
Warning message: In mean.default(L[[1]]) : argument is not numeric or
logical: returning NA
mean(L[[1]]$Number)
#[1] 42.85714
In the for loop, it means we get NAs
for(i in L) mean(i)
# Warning messages:
#1: In mean.default(i) : argument is not numeric or logical: returning NA
#2: In mean.default(i) : argument is not numeric or logical: returning NA
#3: In mean.default(i) : argument is not numeric or logical: returning NA
Once, we extract the column 'Number', the mean works
for(i in L) print(mean(i$Number))
#[1] 42.85714
#[1] 19.42857
#[1] 21
But, it is easier to keep it in the list and update the datasets in the list. Use lapply to create a column 'Average' by looping over the list and getting the mean of the 'Number'
lapply(L, transform, Average = mean(Number))
Or with tidyverse
library(tidyverse)
L %>%
map(~ .x %>%
mutate(Average = mean(Number)))