Given a list of dataframes l
as follows:
l <- list(a = data.frame(a_1 = c(11, 12),
a_2 = c(13, 14)),
b = data.frame(b_1 = c(21, 22),
b_2 = c(23, 24)),
c = data.frame(c_1 = c(31, 32),
c_2 = c(33, 34)))
print(l)
Out:
Now I would like to append a new column new_col
using the name of each dataframe:
l[['a']]$new_col = 'a'
l[['b']]$new_col = 'b'
l[['c']]$new_col = 'c'
I wonder how to append column for each dataframe by using names(l)
which is [1] "a" "b" "c"
automatically? Thanks.
The final result will like this:
for(i in l) {...}
? – Gentle