Suppose I have three columns of data (sample1
, sample2
, and sample3
). I want all of the rows in which the letter b
or h
appears in any one of the columns. This works fine:
data <- data.frame(row_name=c("s1_100","s1_200", "s2_300", "s1_400", "s1_500"),
sample1=rep("a",5),
sample2=c(rep("b",2),rep("a",3)),
sample3=c(rep("a",4),"h")
)
data
# row_name sample1 sample2 sample3
# s1_100 a b a
# s1_200 a b a
# s1_300 a a a
# s1_400 a a a
# s1_500 a a h
bh <- c('b','h')
bh_data <- subset(data, ( sample1 %in% bh | sample2 %in% bh | sample3 %in% bh ) )
bh_data
# row_name sample1 sample2 sample3
# s1_100 a b a
# s1_200 a b a
# s1_500 a a h
However, since I'm asking the same question about each column, isn't there a less redundant way to do this?
But in reality, we have over 800 columns and over 70,000 rows, and we will want to be able to choose as many or as few specific columns to search. Using hundreds of column names for example, just doesn't seem practical unless I script creating the R script.
Try
Or using
data.table
data