[英]list - rename specific data.frames column with lapply
I have got a list with 10 data.frames and I need to rename ONLY one column of each data.frame. 我有一个包含10个data.frames的列表,我只需要重命名每个data.frame的一列。 The column to rename is the no. 要重命名的列是no。 7 and I think I can do the trick with lapply. 7,我想我可以用lapply来解决问题。
Here what I tried without success: 这是我尝试失败的尝试:
lst <- lapply(lst, function(x) colnames(x)[7] <- 'new_name')
I think I am really close to the solution but obviously I am missing something. 我认为我真的很接近解决方案,但显然我缺少一些东西。 Thanks 谢谢
You need to use {}
and return x
: 您需要使用{}
并返回x
:
lst <- lapply(lst, function(x) {colnames(x)[7] <- 'new_name'; x})
Or 要么
lst <- lapply(lst, function(x) {
colnames(x)[7] <- 'new_name'
x
})
As a reproducible example, you could use 作为可重现的示例,您可以使用
lapply(list(iris, iris), function(x) {colnames(x)[3] <- "test"; x})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.