开发者

Getting a subset of an R data frame using values in the order vector

开发者 https://www.devze.com 2023-02-13 09:01 出处:网络
I have a vector of correlation scores myCorVector which contains a range of values from 1 to -1. The vector myCorVector has a score for each row of a data frame myDataFrame.

I have a vector of correlation scores myCorVector which contains a range of values from 1 to -1.

The vector myCorVector has a score for each row of a data frame myDataFrame.

I can order the data frame rows by specifying开发者_JAVA技巧 myDataFrame[order(myCorVector),].

What I would like to do is order the data frame, but retrieve a subset of rows from myDataFrame where myCorVector values are less than 0.

If I apply subset() on myCorVector, then the indices returned by order() no longer associate with valid row indices in myDataFrame.

Is there a quick and/or elegant way to do this that doesn't require a for loop over myCorVector?


Add the scores to the data.frame, create a sorted copy and subset that:

myDataFrame$myCorVector <- myCorVector
odf <- myDataFrame[order(myDataFrame$myCorVector), ]
odf[odf$myCorVector < 0, ]


Simple index should do the job:

myDataFrame[order(myCorVector),][sort(myCorVector)<0,]

The tricky part is the order change imposed by order; without sorting it would be just:

myDataFrame[myCorVector<0,]
0

精彩评论

暂无评论...
验证码 换一张
取 消