Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 202012

Select rows by column value based on range of values in another column in R

$
0
0

I have a dataframe similar to this:

x <- data.frame("A" = c(11:24), 
                "B" = c(25,25,25,25,25,37,37,16,16,16,16,16,42,42), 
                "C" = c(1:3,1:2,1:2,1:3,1:2,1:2))
 A  B C
11 25 1
12 25 2
13 25 3
14 25 1
15 25 2
16 37 1
17 37 2
18 16 1
19 16 2
20 16 3
21 16 1
22 16 2
23 42 1
24 42 2

I want to keep only the rows where each value in B has at least one of all values (1-3) in C. So my result would look like:

 A  B C
11 25 1
12 25 2
13 25 3
14 25 1
15 25 2
18 16 1
19 16 2
20 16 3
21 16 1
22 16 2

I can't seem to get the right keywords in my search for answers.


Viewing all articles
Browse latest Browse all 202012

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>