简体   繁体   中英

Error: $ operator is invalid for atomic vectors for matrix

Here is my code:

datTraits = as.matrix(Phenotype[traitRows, ]);
Col_A = as.data.frame(datTraits$Col_A);

Error in datTraits$Col_A : $ operator is invalid for atomic vectors

I am trying to solve this error but could not find the solution. Thank you for your help!

The 'datTraits' is a matrix . So $ won't work. We can use [

datTraits[, "Col_A"])

In the OP's code, if we are using as.data.frame , note the extraction with $ ie after the )

as.data.frame(datTraits)$Col_A

If we have a matrix with 5 columns and want to subset the first two columns

as.data.frame(datTraits[, 1:2])

If we are subsetting only a single column, to avoid dropping the dimensions, use drop = FALSE

out <- as.data.frame(datTraits[, "Col_A", drop = 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