3

有时在合并之前更改列名的大小写以保持一致性很有用。使用data.framethis 非常简单(如此处所述;尽管相同的解决方案适用于“data.table”,但它会引发警告。例如,

ran <- rep(34,50)
dom <- rep("cat",50)
table <- rep("pig", 50)

DT <- data.table(ran,dom,table); head(DT)
   ran dom table
1:  34 cat   pig
2:  34 cat   pig
3:  34 cat   pig
4:  34 cat   pig
5:  34 cat   pig
6:  34 cat   pig

##the data.frame way

names(DT) <- toupper(names(DT))

##the error 
Warning message:
In `names<-.data.table`(`*tmp*`, value = c("RAN", "DOM", "TABLE" :
  The names(x)<-value syntax copies the whole table. This is due to <- in R 
 itself. Please change to setnames(x,old,new) which does not copy and is faster. 
 See help('setnames'). You can safely ignore this warning if it is inconvenient 
 to change right now. Setting options(warn=2) turns this warning into an error, 
 so you can then use traceback() to find and change your names<- calls.

我使用了以下解决方法来避免错误,并且在宽数据集上速度更快,但是有没有data.table办法做到这一点?

##the work around
upper <- toupper(names(DT))

setnames(DT,upper);head(DT)

   RAN DOM TABLE
1:  34 cat   pig
2:  34 cat   pig
3:  34 cat   pig
4:  34 cat   pig
5:  34 cat   pig
6:  34 cat   pig
4

1 回答 1

5

正如评论所说,给出这个答案setnames是一个data.table功能,并且已经是data.table推荐的方式(正如那个长警告data.table所暗示的那样);例如,

setnames(DT,toupper(names(DT)))

setNames不要与包中的功能混淆stats!(注意大写N)。

于 2013-05-11T00:34:04.763 回答