使用`lapply`重命名列表中的选定列

时间:2019-01-17 02:38:57

标签: r lapply

我试图了解如何在Lapply函数中创建迭代,并且在尝试重命名列表中的列时遇到了问题。请参见下面的可复制示例。

df1 <- data.frame(fruit=c("apple", "Orange", "Pear"), location = c("Japan", "China", "Nigeria"), price = c(32,53,12))
df2 <- data.frame(grocery = c("Durian", "Apple", "Watermelon"), 
                  place=c("Korea", "Japan", "Malaysia"), 
                  name = c("Mark", "John", "Tammy"), 
                  favourite.food = c("Apple", "ORANGE", "Cakes"), 
                  invoice = c("XD1", "XD2", "XD3"))
df3 <- data.frame(address=c("address1", "address2", "address3"), location = c("USA", "UK", "China"))

allobj <- ls()[sapply(ls(),function(t) is.data.frame(get(t)))]
templist <- list(df1, df2, df3)
  

通过简单的for循环,我可以重命名每个数据框中的列名

for (i in seq_along(templist)){
  colnames(templist[[i]]) <- c(paste0("From ", allobj[1]) , paste0("From ", allobj[i+1])) #will change the column name of each dataframe in a list in seq
}
  

但是对于lapply来说,我无法更改每个数据框的第2列。有人可以指出我的错误吗?谢谢!

lapply(seq_along(templist), function(x) {
  i <- seq_along(templist)
  names(x)[2] <- allobj[i]
  x
})

预期产量

[[1]]
   fruit     df1     price
1  apple    Japan    32
2 Orange    China    53
3   Pear  Nigeria    12

[[2]]
     grocery    df2    name favourite.food invoice
1     Durian    Korea  Mark          Apple     XD1
2      Apple    Japan  John         ORANGE     XD2
3 Watermelon Malaysia Tammy          Cakes     XD3

[[3]]
   address     df3
1 address1      USA
2 address2       UK
3 address3    China

1 个答案:

答案 0 :(得分:1)

在这种情况下,我建议改用mapply

mapply(function(x, y) {names(x)[2] <- y; x}, templist, allobj)

#[[1]]
#   fruit     df1 price
#1  apple   Japan    32
#2 Orange   China    53
#3   Pear Nigeria    12

#[[2]]
#     grocery      df2  name favourite.food invoice
#1     Durian    Korea  Mark          Apple     XD1
#2      Apple    Japan  John         ORANGE     XD2
#3  Watermelon Malaysia Tammy         Cakes     XD3

#[[3]]
#   address   df3
#1 address1   USA
#2 address2    UK
#3 address3 China

如果您想使用lapply,则可以使用x作为同时templistallobj的子集的索引,因为对于第一个列表,我们希望从第一个值开始命名的allobj中,对于第二个列表,我们需要allobj的第二个值,依此类推。

lapply(seq_along(templist), function(x) {
   names(templist[[x]])[2] <- allobj[x]
   templist[[x]]
 })