kpe*_*ton 3 r bigdata data.table
有时,在合并之前更改列名的大小写以保持一致性很有用。使用 a 时,data.frame这非常简单(如此处所述);尽管相同的解决方案适用于“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.
Run Code Online (Sandbox Code Playgroud)
我使用了以下解决方法来避免错误,并且在广泛的数据集上速度要快得多,但是有没有办法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
Run Code Online (Sandbox Code Playgroud)
正如评论所说,给出这个答案setnames是一种data.table功能,并且已经是data.table推荐的方式(正如来自data.table建议的长警告);例如,
setnames(DT,toupper(names(DT)))
Run Code Online (Sandbox Code Playgroud)
不要与包setNames中的功能混淆stats!(注意大写N)。