简体   繁体   中英

Searching for a list of string in a dataframe in R

I have a list of names, and a data.frame with many different columns. How can I retrieve rows in the data frame that their row.name is one of the names in my list?

For example if the row.names in my data frame has many rows, including TC09001536.hg.1 , TC03002852.hg.1 , and TC18000664.hg.1 names, which are saved in list called Top.list . Assuming my data frame is called df then I tried:

test <- df[grep(Top.list, df$cluster_id),]

to look within cluster_id column and if matches the names in my list then give me whole rows.

This should work:

test <- df[unlist(lapply(Top.list, function(x) grep(x, df$cluster_id, fixed = TRUE))),]

The lapply(Top.list, function(x) grep(x, df$cluster_id, fixed = TRUE)) part generates a list with vectors of matching row numbers for each of your words, the unlist combines the vectors to one vector, from which your dataframe will be subsetted.

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