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

Combine group_by, ifelse and filter

$
0
0

I would like to combine group_by, ifelse and filter my code for the example dataframe below. What I would like is the following: 1) Group by x. 2) Check if result > 1. If TRUE, check if month for which result >1 == max(month) for that group. If TRUE, select all rows for that group. All other rows should be discarded (so both in case result <= 1 or (month where result > 1 != max(month)) . So in my example data frame all rows for B should be kept and all rows for A should be discarded.

   x month result
1  A     1    0.5
2  A     2    0.6
3  A     3    1.2
4  A     4    1.1
5  A     5    0.9
6  B     1    0.3
7  B     2    0.4
8  B     3    0.5
9  B     4    0.9
10 B     5    1.2


dat <- data.frame(x = c("A","A","A","A","A","B","B","B","B","B"),
                  month = c(1,2,3,4,5,1,2,3,4,5),
                  result = c(.5,.6,1.2,1.1,.9,.3,.4,.5,.9,1.2))

Viewing all articles
Browse latest Browse all 201839

Trending Articles



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