I have a data table with a bunch of columns, e.g.:
dt<-data.table(matrix(runif(10*10),10,10))
I want to perform some operation on the data table, such as producing a correlation matrix (cor(dt)
). In order to do this, I want to remove a few columns that contain non-numeric values or values outside a certain range.
Let's say I want to find the correlation matrix excluding V1, V2, V3 and V5.
Here is my current approach:
cols<-!(colnames(dt)=="V1" | colnames(dt)=="V2" | colnames(dt)=="V3" | colnames(dt)=="V5")
new_dt<-subset(dt,,cols)
cor(new_dt)
I find this pretty cumbersome, considering data.table syntax is usually so elegant. Is there a better method of doing this?