重命名多个数据帧中的列R [英] Rename columns in multiple dataframes, R
问题描述
我正在尝试重命名多个 data.frame
s的列。
I am trying to rename columns of multiple data.frame
s.
举个例子,让我们说我有一个列表 data.frame
s dfA
, dfB
和 dfC
。我写了一个函数 changeNames
相应地设置名称,然后使用 lapply
如下:
To give an example, let's say I've a list of data.frame
s dfA
, dfB
and dfC
. I wrote a function changeNames
to set names accordingly and then used lapply
as follows:
dfs <- list(dfA, dfB, dfC)
ChangeNames <- function(x) {
names(x) <- c("A", "B", "C" )
}
lapply(dfs, ChangeNames)
但是,这不按预期工作。似乎我没有将新名称分配给 data.frame
,而只创建新名称。我在这里做错了什么?
However, this doesn't work as expected. It seems that I am not assigning the new names to the data.frame
, rather only creating the new names. What am I doing wrong here?
提前谢谢!
推荐答案
p>这里有两件事:
-
1)你应该从你的函数返回你想要的值。否则,将返回最后一个值。在你的情况下,这是
names(x)
。所以,您应该添加最后一行,return(x)
或简单地x
。所以,你的功能如下所示:
1) You should return the value you want from your function. Else, the last value will be returned. In your case, that's
names(x)
. So, instead you should add as the final line,return(x)
or simplyx
. So, your function would look like:
ChangeNames <- function(x) {
names(x) <- c("A", "B", "C" )
return(x)
}
2) lapply
不会通过引用修改输入对象。它在一个副本上工作。所以你必须重新分配结果。或者另一个选择是使用 for-loops
而不是 lapply
:
2) lapply
does not modify your input objects by reference. It works on a copy. So, you'll have to assign the results back. Or another alternative is to use for-loops
instead of lapply
:
# option 1
dfs <- lapply(dfs, ChangeNames)
# option 2
for (i in seq_along(dfs)) {
names(dfs[[i]]) <- c("A", "B", "C")
}
即使使用 for循环
,您仍然会复制一份(因为名称(。)< - 。
)。您可以使用 tracemem
来验证这一点。
Even using the for-loop
, you'll still make a copy (because names(.) <- .
does). You can verify this by using tracemem
.
df <- data.frame(x=1:5, y=6:10, z=11:15)
tracemem(df)
# [1] "<0x7f98ec24a480>"
names(df) <- c("A", "B", "C")
tracemem(df)
# [1] "<0x7f98e7f9e318>"
如果要通过引用修改,可以使用 data.table
package's setnames
function:
If you want to modify by reference, you can use data.table
package's setnames
function:
df <- data.frame(x=1:5, y=6:10, z=11:15)
require(data.table)
tracemem(df)
# [1] "<0x7f98ec76d7b0>"
setnames(df, c("A", "B", "C"))
tracemem(df)
# [1] "<0x7f98ec76d7b0>"
你看到内存位置 df
映射到没有改变。这些名称已通过参考修改。
You see that the memory location df
is mapped to hasn't changed. The names have been modified by reference.
这篇关于重命名多个数据帧中的列R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!