Adding a new column to each element in a list of tables or data frames

前端 未结 6 1666
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 03:51

I have a list of files. I also have a list of \"names\" which I substr() from the actual filenames of these files. I would like to add a new column to each of t

6条回答
  •  不知归路
    2020-11-27 04:03

    data_lst <- list(
      data_1 = data.frame(c1 = 1:3, c2 = 3:1),
      data_2 = data.frame(c1 = 1:3, c2 = 3:1)
    )
    
    f <- function (data, name){
      data$name <- name
      data
    }
    
    Map(f, data_lst , names(data_lst)) 
    

提交回复
热议问题