1

I have a list of data frames. I want to add a new column to each data frame. For example, I have three data frames as follows:

a = data.frame("Name" = c("John","Dor"))
b = data.frame("Name" = c("John2","Dor2"))
c = data.frame("Name" = c("John3","Dor3"))

I then put them into a list:

dfs = list(a,b,c)

I then want to add a new column with a unique value to each data frame, e.g.:

dfs[1]$new_column <- 5

But I get the following error:

"number of items to replace is not a multiple of replacement length"

I have also tried using two brackets:

dfs[[1]]$new_column <- 5

This does not return an error but it does not add the column.

This would be in a 'for' loop and a different value would be added to each data frame.

Any help would be much appreciated. Thanks in advance!

jtam
  • 576
  • 1
  • 4
  • 15

1 Answers1

1

Let's say you want to add a new column with value 5:7 for each dataframe. We can use Map

new_value <- 5:7
Map(cbind, dfs, new_column = new_value)

#[[1]]
#  Name new_column
#1 John          5
#2  Dor          5

#[[2]]
#   Name new_column
#1 John2          6
#2  Dor2          6

#[[3]]
#   Name new_column
#1 John3          7
#2  Dor3          7

With lapply you could do

lapply(seq_along(dfs), function(i) cbind(dfs[[i]], new_column = new_value[i]))

Or as @camille mentioned it works if you use [[ for indexing in the for loop

for (i in seq_along(dfs)) {
    dfs[[i]]$new_column <- new_value[[i]]
}

The equivalent purrr version of this would be

library(purrr)
map2(dfs, new_value, cbind)

and

map(seq_along(dfs), ~cbind(dfs[[.]], new_colum = new_value[.]))
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
  • Thanks for this! I have a question for you, however: when I use the double brackets the initial data frame (e.g., "a") does not get a column added, but the "a" within the list does get the column added. Why is that? – jtam Jul 15 '19 at 03:22
  • @jtam because `dfs` is a separate object and `a` is a separate object. In the `for` loop we are only adding a column for `dfs` object and it is independent of `a`. – Ronak Shah Jul 15 '19 at 03:31
  • Thanks for the explanation -- appreciate it! – jtam Jul 19 '19 at 15:55