如何检测具有至少一个错位数字的个体

huangapple go评论65阅读模式
英文:

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&lt;-c(1,1,1,1,
      2,2,2,
      3,3,
      4,4,4,4)

order&lt;-c(44,44,45,41,
        13,21,2013,
        21,10,
        45,65,74,877)

mydata&lt;-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)))&lt;0)$ID)
[1] 1 3

or if you want the rows back:

subset(mydata, ave(order, ID, FUN=\(x)c(0, diff(x)))&lt;0)
  ID order
4  1    41
9  3    10

library(dplyr) # version &gt;= 1.1.0

mydata %&gt;% 
   filter(order&lt;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 %&gt;% 
  filter(row_number() != rank(order, ties.method = &quot;first&quot;), .by = ID)

#&gt; # A tibble: 6 &#215; 2
#&gt; # Groups:   ID [2]
#&gt;      ID order
#&gt;   &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1     1    44
#&gt; 2     1    44
#&gt; 3     1    45
#&gt; 4     1    41
#&gt; 5     3    21
#&gt; 6     3    10

To just get the ID of the individuals we can add pull(ID) %&gt;% unique() to the pipe:

mydata %&gt;% 
  filter(row_number() != rank(order, ties.method = &quot;first&quot;), .by = ID) %&gt;% 
  pull(ID) %&gt;% 
  unique()
#&gt; 1 3

Data from OP

ID&lt;-c(1,1,1,1,
      2,2,2,
      3,3,
      4,4,4,4)

order&lt;-c(44,44,45,41,
         13,21,2013,
         21,10,
         45,65,74,877)

mydata&lt;-data.frame(
  ID=ID,order=order
)

<sup>Created on 2023-03-09 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月9日 19:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定