Home > Blockchain >  Extract specific columns based on target list ,
Extract specific columns based on target list ,

Time:09-08

I have a data set comprising 1000 rows and 5000 columns, and I want to select specific columns ("mydata"). The target columns are listed in the data frame "cols". I've tried some of the examples posted here, but no success! Could anyone please suggest it to me?

 cols[1:5,]
 c("S100024810", "S100024905", "S100024920", "S100024923", "S100025437"
 )
 
 mydata[1:3,1:5]
 structure(list(S100024732 = c("1", "0", "1"), S100024733 = c("-", 
 "0", "0"), S100024803 = c("0", "0", "-"), S100024810 = c("1", 
 "1", "1"), S100024817 = c("-", "1", "-")), row.names = c(NA, 
 3L), class = "data.frame")
 
 mydata[,cols[cols %in% names(mydata)]]
 Error in .subset(x, j) : invalid subscript type 'list'

CodePudding user response:

cols appears to be a data frame with one column, so you need to reference for that column.

cols[cols[, 1] %in% names(mydata), 1]
# [1] "S100024810"

To prevent the result to be coerced to a vector if it's just one column (as it happens with your cols data frame), we need to do drop=FALSE.

mydata[, cols[cols[, 1] %in% names(mydata), 1], drop=FALSE]
#   S100024810
# 1          1
# 2          1
# 3          1

Data:

cols <- structure(list(V1 = c("S100024810", "S100024905", "S100024920", 
"S100024923", "S100025437")), class = "data.frame", row.names = c(NA, 
-5L))

mydata <- structure(list(S100024732 = c("1", "0", "1"), S100024733 = c("-", 
"0", "0"), S100024803 = c("0", "0", "-"), S100024810 = c("1", 
"1", "1"), S100024817 = c("-", "1", "-")), row.names = c(NA, 
3L), class = "data.frame")
  •  Tags:  
  • r
  • Related