Append a new column using the name of dataframe from list of dataframe in R [duplicate]
Asked Answered
G

2

5

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:

enter image description here

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:

enter image description here

Gentle answered 29/7, 2021 at 8:25 Comment(0)
S
6

Method 1: tidyverse solution with map2

library(tidyverse)
map2(l, names(l), ~ mutate(.x, new_col = .y))

Output:

$a
  a_1 a_2 new_col
1  11  13       a
2  12  14       a

$b
  b_1 b_2 new_col
1  21  23       b
2  22  24       b

$c
  c_1 c_2 new_col
1  31  33       c
2  32  34       c

Method 2: for loop solution

(l gives the above output):

for(i in 1:length(names(l))) {
  l[[i]]$new_col <- names(l)[i]
}
Sy answered 29/7, 2021 at 8:33 Comment(2)
Thanks, it works. Is it possible to using for loop such as for(i in l) {...}?Gentle
@ahbon absolutely: just edited my answer to add a for loop solution. map2 does the same thing and is cleaner in my opinion, but use whichever you like.Sy
W
2

Or with mapply():

mapply(cbind, l, "new_col"=names(l), SIMPLIFY=F)
Wrens answered 29/7, 2021 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.