R select rows in matrix from another vector (match, %in)

拈花ヽ惹草 提交于 2020-01-25 01:57:12

问题


Say I have a dataframe with 6 columns and 100000 rows. I want to select rows in matrix originScen based on the indices/numbers in another vector reducedScenIds (10,000 rows). I select the rows by checking if the value of each member of Y matches the value in column 1 of the dataframe X. Now the first column can have multiple matches for each value of Y.

So I used the below

reducedSet <- originScen[which(originScen[,1] %in% reducedScenarioIds),]

I am ok with the results except that which and %in% seems to destroy the order of reducedScenarioIds vector. The final reducedSet has rows selected based on ascending order of ids found in the reducedScenarioIds vector and not the exact same order.

The originScen[,1] can have duplicate entries for each entry in reducedScenarioIds

Anyone have an alternate solution?

Thanks


回答1:


Try this:

reducedSet <- originScen[originScen[,1] %in% reducedScenarioIds,][order(na.exclude(match(originScen[,1], reducedScenarioIds))),]


来源:https://stackoverflow.com/questions/17451634/r-select-rows-in-matrix-from-another-vector-match-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!