有时在合并之前更改列名的大小写以保持一致性很有用。使用data.frame
this 非常简单(如此处所述);尽管相同的解决方案适用于“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