简体   繁体   中英

R: subsetting issue with some datatables when using column value/position for subsetting

With some data.table i am facing subsetting issue.

for example if i need 4th,8th and 9th column of a particular data table "y" i used below code, but as a result it is creating vector of 4,8,9 instead of getting 4th,8th and 9th columns

y<-data.table(rnorm(100),pnorm(100),rnorm(100),pnorm(100),rnorm(100),pnorm(100),rnorm(100),pnorm(100),rnorm(100),pnorm(100))
req<-y[,c(4,8,9)]
req

for the above code result is an vector output of 4,8,9

# [1] 4 8 9

for only some data table i am facing this issue.Can anyone please resolve this ?

Try

req<-y[,c(4,8,9),with=FALSE]

I think it will give you what you want

At least two options here:

# using column name
y[ , .(V4,V8,V9)]

# using column position
y[, c(4,8,9), with = FALSE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM