Reputation: 513
I have a list of names (mylist). This list of names coincide with some of the row names in a (much) bigger file, which also contain additional columns of data (bigfile). How can I extract the rows in 'bigfile' matching with the names in 'mylist'?
Upvotes: 0
Views: 7063
Reputation: 1
Extract subset of a matrix given rownames.. Below snip, extracts subset from users matrix, where rownames match with given list of users extracted from a data frame activity_data_t
r_users = users[rownames(users) %in% unique(activity_data_t[activity_data_t$role_id == target_latest_role, "user_id"]),]
Upvotes: 0
Reputation: 549
# collect the names from the bigfile
BigFileNames <- colnames(bigfile)
# find the intersect between your list and the list of names from Bigfile
common <- intersect(BigFileNames, myListNames)
# extract info from dataframe
infoFromCommon <- bigfile[,match(common, myListNames)]
Upvotes: 1
Reputation: 2827
A great place to look would be Hadley's page on subsetting in R.
In terms of a specific answer to the question, let's assume that you have an original list of rownames that you would like to subset on that is called 'mylist'. You could do the following:
index <- row.names(bigfile) %in% mylist[,1]
This should give a boolean expression that is as long as the number of rows in the bigfile.
EDIT: You might also look at the following Stackoverflow post which nicely addresses the problem.
Upvotes: 2