长数据——当每个人的第一个值=X R时,删除所有行

时间:2021-11-18 20:06:08

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.

我有一个类似于下面的长数据框。我想把dataframe限制在年龄小于65岁的人的第一次观察。注意,苏在研究过程中已经65岁了,我不想删除她的任何数据。这是因为她的第一个值<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.

我试图让一个函数....我想,如果我可以为每个人的第一个值是>65做一个指示器变量,那么我就可以排除那个人行。

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?

有人能解释一下吗?

1 个解决方案

#1


3  

You may use

你可以用

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

Or using data.table

或者使用data.table

library(data.table)
setDT(df)[, .SD[string[1L] <65] , id]

#1


3  

You may use

你可以用

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

Or using data.table

或者使用data.table

library(data.table)
setDT(df)[, .SD[string[1L] <65] , id]