Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long data - remove all rows when first value for each person =X R

Tags:

r

long-integer

I have a long data frame similar to below. I want to restrict the dataframe to people who were aged <65 yrs on their first observation. Note sue turns 65 during the course of the study, I don't want to remove any of her data. This is because her first value is <65.

I tried to make a function.... I thought that if I could make an indicator variable for when each person's first value is >65 then I could exclude that persons rows.

df$first<-ave(df$string, df$id, FUN=function(x) [1]>65)  ##wrong!



 id string
1    pat     71
2    pat     72
3    pat     73
4    pat     74
5    tom     51
6    tom     52
7    tom     53
8    tom     54
9    sue     63
10   sue     64
11   sue     65
12   sue     66
13  mary     68
14  mary     69
15  mary     70
16  mary     71
17 harry     17
18 harry     18
19 harry     19
20 harry     20

Can anyone shed any light on this please?

like image 563
user2363642 Avatar asked Feb 01 '26 18:02

user2363642


1 Answers

You may use

df[as.logical(with(df, ave(string, id, FUN= function(x) x[1] < 65))),]

Or using data.table

library(data.table)
setDT(df)[, .SD[string[1L] <65] , id]
like image 125
akrun Avatar answered Feb 04 '26 07:02

akrun



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!