英文:
How to detect individuals who have at least one misordered number
问题
这是我的数据集表示:
ID<-c(1,1,1,1,
2,2,2,
3,3,
4,4,4,4)
order<-c(44,44,45,41,
13,21,2013,
21,10,
45,65,74,877)
mydata<-data.frame(
ID=ID, order=order
)
变量order
应该随着时间增加。通过程序,我应该检测所有在随访时变量order
减少的个体(在本例中为ID=1和ID=3)。
英文:
Here is a representation of my dataset:
ID<-c(1,1,1,1,
2,2,2,
3,3,
4,4,4,4)
order<-c(44,44,45,41,
13,21,2013,
21,10,
45,65,74,877)
mydata<-data.frame(
ID=ID,order=order
)
ID Time order
1 1 1 44
2 1 2 44
3 1 3 45
4 1 4 41
5 2 1 13
6 2 2 21
7 2 3 2013
8 3 1 21
9 3 2 10
10 4 1 45
11 4 2 65
12 4 3 74
13 4 4 877
The variable order
should increase with time.
With a program, I should detect all the individuals (ID=1 and ID=3 in this case) that have variable order
that decreases at a time of follow-up.
答案1
得分: 2
unique(subset(mydata, ave(order, ID, FUN=function(x) c(0, diff(x))) < 0)$ID)
[1] 1 3
or if you want the rows back:
subset(mydata, ave(order, ID, FUN=function(x) c(0, diff(x))) < 0)
ID order
4 1 41
9 3 10
library(dplyr) # version >= 1.1.0
mydata %>%
filter(order < lag(order), .by=ID)
ID order
1 1 41
2 3 10
英文:
unique(subset(mydata, ave(order, ID, FUN=\(x)c(0, diff(x)))<0)$ID)
[1] 1 3
or if you want the rows back:
subset(mydata, ave(order, ID, FUN=\(x)c(0, diff(x)))<0)
ID order
4 1 41
9 3 10
library(dplyr) # version >= 1.1.0
mydata %>%
filter(order<lag(order), .by=ID)
ID order
1 1 41
2 3 10
答案2
得分: 2
根据Roland的评论,请检查`is.unsorted`函数:
```r
unique(mydata$ID[with(mydata, ave(order, ID, FUN = is.unsorted)) == 1])
#[1] 1 3
或者
subset(mydata, ave(order, ID, FUN = is.unsorted) == 1)
# ID order
#1 1 44
#2 1 44
#3 1 45
#4 1 41
#8 3 21
#9 3 10
在dplyr
中:
library(dplyr) #1.1.0
filter(mydata, is.unsorted(order), .by = ID)
在data.table
中:
setDT(mydata)[, .SD[is.unsorted(order)], by = ID]
英文:
As per Roland's comment, check is.unsorted
:
unique(mydata$ID[with(mydata, ave(order, ID, FUN = is.unsorted)) == 1])
#[1] 1 3
or
subset(mydata, ave(order, ID, FUN = is.unsorted) == 1)
# ID order
#1 1 44
#2 1 44
#3 1 45
#4 1 41
#8 3 21
#9 3 10
In dplyr
:
library(dplyr) #1.1.0
filter(mydata, is.unsorted(order), .by = ID)
In data.table
:
setDT(mydata)[, .SD[is.unsorted(order)], by = ID]
答案3
得分: 1
我们可以检查rank(order)
是否对应于row_number()
:
library(dplyr)
mydata %>%
filter(row_number() != rank(order, ties.method = "first"), .by = ID)
#> # A tibble: 6 × 2
#> # Groups: ID [2]
#> ID order
#> <dbl> <dbl>
#> 1 1 44
#> 2 1 44
#> 3 1 45
#> 4 1 41
#> 5 3 21
#> 6 3 10
要仅获取个体的ID
,我们可以在管道中添加 pull(ID) %>% unique()
:
mydata %>%
filter(row_number() != rank(order, ties.method = "first"), .by = ID) %>%
pull(ID) %>%
unique()
#> 1 3
数据来自 OP
ID<-c(1,1,1,1,
2,2,2,
3,3,
4,4,4,4)
order<-c(44,44,45,41,
13,21,2013,
21,10,
45,65,74,877)
mydata<-data.frame(
ID=ID,order=order
)
创建于 2023-03-09,使用 reprex v2.0.2
英文:
We could check if the rank(order)
corresponds to the row_number()
:
library(dplyr)
mydata %>%
filter(row_number() != rank(order, ties.method = "first"), .by = ID)
#> # A tibble: 6 × 2
#> # Groups: ID [2]
#> ID order
#> <dbl> <dbl>
#> 1 1 44
#> 2 1 44
#> 3 1 45
#> 4 1 41
#> 5 3 21
#> 6 3 10
To just get the ID
of the individuals we can add pull(ID) %>% unique()
to the pipe:
mydata %>%
filter(row_number() != rank(order, ties.method = "first"), .by = ID) %>%
pull(ID) %>%
unique()
#> 1 3
Data from OP
ID<-c(1,1,1,1,
2,2,2,
3,3,
4,4,4,4)
order<-c(44,44,45,41,
13,21,2013,
21,10,
45,65,74,877)
mydata<-data.frame(
ID=ID,order=order
)
<sup>Created on 2023-03-09 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论