首页 > 解决方案 > 独特读者之间的比较

问题描述

代表

dat <- data.frame(id = c(1,1,2,2,3,3,4,4), 
                  reader = c(1,4,2,3,3,4,2,5), 
                  response = c("CR","PR","SD","SD","PR","PR","CR","SD"))

问题:希望response在每个独特之处reader进行比较id。总共有 5 个独特readers的,但每个id只有 2 个个体readers

生成的数据集将如下所示:

# A tibble: 4 x 4
     id read1 read2 matchflag
  <dbl> <chr> <chr>     <dbl>
1     1 CR    PR            0
2     2 SD    SD            1
3     3 PR    PR            1
4     4 CR    SD            0

标签: rdataframe

解决方案


一个data.table选项

dcast(
  setDT(df),
  id ~ paste0("reader", rowid(id)),
  value.var = "response"
)[
  ,
  match_flag := +(reader1 == reader2)
][]

   id reader1 reader2 match_flag
1:  1      CR      PR          0
2:  2      SD      SD          1
3:  3      PR      PR          1
4:  4      CR      SD          0

推荐阅读