列表-使用lapply重命名特定的data.frames列 [英] list - rename specific data.frames column with lapply

查看:58
本文介绍了列表-使用lapply重命名特定的data.frames列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10个data.frames的列表,我只需要重命名每个data.frame的一列.要重命名的列是no. 7,我想我可以用lapply来解决问题.

I have got a list with 10 data.frames and I need to rename ONLY one column of each data.frame. The column to rename is the no. 7 and I think I can do the trick with 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

推荐答案

您需要使用{}并返回x:

lst <- lapply(lst, function(x) {colnames(x)[7] <- 'new_name'; x}) 

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})

这篇关于列表-使用lapply重命名特定的data.frames列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆