data.table中的setnames()会覆盖原始名称的原子矢量

时间:2016-02-12 12:09:25

标签: r reference data.table names shallow-copy

我经常使用data.table,接收带有本地列名的数据,这些数据存储起来供以后使用。然后我重命名data.table对象 这样我就可以更有效地对数据进行子集化,然后恢复原始名称。但是我发现data.table实际上会覆盖原始名称,即使存储为矢量。

这是一个让事情变得更清晰的例子:

# Get libraries
library(data.table)
library(ggplot2)

# Data table for demo
data <- data.table("Bad names" = 1:100, "Shouldn't use spaces" = rep(c("text","other"),100))

# Store the original names
# Changing this to c(names(data)) solves the problem but is a hack
original.names <- names(data) 

# Update the data's column names
setnames(data, old = names(data),new = c("V1","V2"))

# Now original.names has also been changed to the new names
# However, only the names of the data should have changed.
cat(original.names,"\n", names(data))

# Random ggplot
ggplot(data) + geom_bar(aes(V1))

# Rename data
setnames(data, names(data), original.names)

# I still get names V1 and V2, it is as if original names has been changed globally.
cat(names(data))

我已经注意到问题的解决方案是将names(data)包裹在c()中。为什么这样做?是否有一种更有效,更有效的方法来解决这个问题?

感谢您的帮助。

0 个答案:

没有答案