Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain only one line per factor if it meets criteria R

Tags:

r

I have a dataset that looks like this:

ID  week  action
1   1     TRUE
1   1     FALSE
1   2     FALSE 
1   2     FALSE
1   3     FALSE
1   3     TRUE
2   1     FALSE
2   2     TRUE
2   2     FALSE
...

What I'd like to do is retain for each ID and each week within ID, one value of action, with preference to retaining a TRUE if there is one, else a FALSE.

So it would look like this when through:

ID  week  action
1   1     TRUE
1   2     FALSE
1   3     TRUE
2   1     FALSE
2   2     TRUE
...
like image 251
gh0strider18 Avatar asked Dec 04 '25 10:12

gh0strider18


1 Answers

I used plyr:

library(plyr)
ddply(df,.(ID,week),summarize,action=sum(action)>0)
like image 147
Sam Dickson Avatar answered Dec 07 '25 06:12

Sam Dickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!