当前位置: 动力学知识库 > 问答 > 编程问答 >

matrix - How to select rows based on their column value in R

问题描述:

the dataset is something like this:

daily.sample

day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12 day13 day14

132304 919 420 1021 331 1012 606 256 58 302 87 859 1140 603 1472

121945 419 1048 541 336 1086 796 95 578 1419 985 752 570 865 242

130055 3036 1473 3043 3291 4869 2902 5306 4480 4820 254 2652 2529 3346 995

I want to select rows that satisfy the following conditions:

it's day1 value is close to it's day8 value

it's day2 value is close to it's day9 value

it's day3 value is close to it's day10 value

...

by close i mean the difference is within a range, or something like

"0.9< day1/day8 <1.1"

网友答案:

You can simply substract the right part of your data.frame from the left part by:

distance <- abs(daily.sample[ , 1:7, drop = FALSE ] - daily.sample[ , 8:14, drop  =FALSE ])

The ratio is build accordingly:

ratio <- daily.sample[ , 1:7, drop = FALSE ] / daily.sample[ , 8:14, drop  =FALSE ]

And then you can simply subset your data depending on the day for which you want to check.

daily.sample[ distance[ ,"day1" ] < 1000, ]

       day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12 day13 day14
132304  919  420 1021  331 1012  606  256   58  302    87   859  1140   603  1472
121945  419 1048  541  336 1086  796   95  578 1419   985   752   570   865   242

Or for the ratio within a range:

daily.sample[ ratio[ , "day1"] > 0.7 & ratio[ , "day1"] < (10/7),  ]


       day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12 day13 day14
121945  419 1048  541  336 1086  796   95  578 1419   985   752   570   865   242

If for the latter case the range is always (min.ratio, 1/min.ratio) you could calculate the ratio as ratio[ ratio < 1 ] <- 1 / ratio[ ratio < 1 ] and than just subset by:

daily.sample[ ratio[ , "day1"] < (10/7),  ].
分享给朋友:
您可能感兴趣的文章:
随机阅读: