Couple of solutions:
Subsetting based on names
newDataFrame <- expressionData[!(names(expressionData) %in% patient_names)]
One problem with your attempt was that you hadn't wrapped the whole expression evaluated by !
in parentheses. As it was, you were looking for !names(expressionData)
in patient_names
. !
here would coerce names(expressionData)
into a logical and likely return a vector full of FALSE
s
I've subset with only one dimension (x[this]
rather than x[,this]
). You can do this with the columns of data frames because a data frame is a list of its columns. This subsetting method preserves the data.frame
class of the returned object, whereas the two-dimensional subset will just return a vector if you select only one column. (Tibbles will return a tibble with both methods, which is one big advantage of tibbles)
Tidyverse solution: use dplyr::select
with dplyr::all_of
newDataFrame <- dplyr::select(expressionData, -dplyr::all_of(patientnames))
Edit: Make sure your data really is a data.frame
If you're getting this error Error in UseMethod("select_") : no applicable method for 'select_' applied to an object of class "c('matrix', 'array', 'double', 'numeric')"
, it's because your data is a matrix, rather than a data frame. You may have inadvertently coerced it in processing.
Use as.data.frame
to return to a data frame object, which will be compabtible with the methods above. If you wish to keep your data as a matrix, use colnames
:
expressionData[ , !(colnames(expressionData) %in% patient_names)]
to subset the columns.
If expressionData
is a matrix, you'll need to subset the columns with colnames
, rather than names
. The names
of a data.frame are identical to its colnames
(because a df is a list of its columns), but the names
of a matrix are the names of every element in the matrix, because a matrix is just an array with dimensionality. You'll want to check colnames(expressionData)
to make sure that there are colnames
to subset.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…