Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 多个摘要计数和一个跨可变行数的标志_R_Aggregate_Summarize - Fatal编程技术网

R 多个摘要计数和一个跨可变行数的标志

R 多个摘要计数和一个跨可变行数的标志,r,aggregate,summarize,R,Aggregate,Summarize,我有以下的出发点: id.s <- c(1,1,2,2,2,3,3,3,3,4,4,4) test.s <- c("Negative", "Positive", "Positive", "Negative", "Positive", "Negative", "Negative", "Negative", &qu

我有以下的出发点:

id.s <- c(1,1,2,2,2,3,3,3,3,4,4,4)
test.s <- c("Negative", "Positive", "Positive", "Negative", "Positive",
        "Negative", "Negative", "Negative", "Positive", "Negative",
        "Negative", "Negative")
Start <- as.data.frame(cbind(id.s,test.s))

id.s我们可以按“id.s”进行分组,使用
summary
获得行数(
n()
),然后检查%
'test.s'中是否有任何
阳性的
%

library(dplyr)
Start %>%
   group_by(id.s) %>%
   summarise(NumberOfVisits = n(),
             Positive.Test = c('No', 'Yes')[1 + ('Positive' %in% test.s)], 
            Num.Positive = sum(test.s == 'Positive'), .groups = 'drop')
-输出

# A tibble: 4 x 4
#  id.s  NumberOfVisits Positive.Test Num.Positive
#  <chr>          <int> <chr>                <int>
#1 1                  2 Yes                      1
#2 2                  3 Yes                      2
#3 3                  4 Yes                      1
#4 4                  3 No                       0
#一个tible:4 x 4
#身份证号码为阳性。测试号码为阳性
#                             
#1 1 2是1
#2 2 3是2
#3 3 4是1
#四四三零

A
数据。带有
dcast的表
选项

dcast(
  setDT(Start), id.s ~ test.s
)[
  , `:=`(
    NumVisits = rowSums(.SD),
    PostiveTest = c("No", "Yes")[1 + (Positive > 0)]
  ),
  .SDcols = -1
][
  , Negative := NULL
][]
给予


非常感谢!我知道这涉及到将一些函数串在一起,但我自己无法实现。这对我的数据非常有效。虽然我采用了第一种解决方案,只是因为我更容易理解dplyr代码,但我确实确认上述方法也能很好地处理我的数据。因此,对于后面和之前的解决方案对于data.table解决方案,这绝对符合要求。谢谢。
dcast(
  setDT(Start), id.s ~ test.s
)[
  , `:=`(
    NumVisits = rowSums(.SD),
    PostiveTest = c("No", "Yes")[1 + (Positive > 0)]
  ),
  .SDcols = -1
][
  , Negative := NULL
][]
   id.s Positive NumVisits PostiveTest
1:    1        1         2         Yes
2:    2        2         3         Yes
3:    3        1         4         Yes
4:    4        0         3          No