R name colnames and rownames in list of data.frames with lapply

后端 未结 2 1692
后悔当初
后悔当初 2020-12-16 07:51

I\'m pretty frustrated because I dont know how I achieve the naming of the columns and rows in a list of data.frames. I mean I want to avoid using a loop. So I figured I cou

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 08:27

    You need to remember that the object x inside the lapply is not the original object, but a copy. Changing the colnames of the copy does not impact the original object. You need to return x in order to get a new copy of the object that includes the new names.

    new_obj = lapply(a, function(x) {
       colnames(x) <- paste("col",1:10,sep="")
       return(x)
      })
    

提交回复
热议问题