【问题标题】:rowsums with multiple conditions具有多个条件的行和
【发布时间】:2019-06-20 06:02:14
【问题描述】:

我正在尝试计算具有多个变量的行中的累积总和。

这是我的数据作为示例。我有 5 个患者 ID 和 4 个条件变量。如果条件中有 '1 到 3' 之间的值,则 cumsum 将加 1。

ID<-c("a","b","c","d","e")
cond1<-as.factor(sample(x=1:7,size=5,replace=TRUE))
cond2<-as.factor(sample(x=1:7,size=5,replace=TRUE))
cond3<-as.factor(sample(x=1:7,size=5,replace=TRUE))
cond4<-as.factor(sample(x=1:7,size=5,replace=TRUE))
df<-data.frame(ID,cond1,cond2,cond3,cond4)
df

  ID cond1 cond2 cond3 cond4
1  a     2     7     6     6
2  b     7     2     3     6
3  c     4     3     1     4
4  d     7     3     3     6
5  e     6     7     7     3

我使用rowSums 代码和以下语句。然而,作为第 2 行,虽然 cond2 是 2,cond3 是 3,但 cumsum 不是“2”,“1”。第 4 行也有同样的问题。

df$cumsum<-rowSums(df[,2:5]==c(1,2,3),na.rm=TRUE)
df
  ID cond1 cond2 cond3 cond4 cumsum
1  a     2     7     6     6      0
2  b     7     2     3     6      1
3  c     4     3     1     4      1
4  d     7     3     3     6      1
5  e     6     7     7     3      0

如何使其累积?非常感谢您的帮助。

【问题讨论】:

    标签: r cumulative-sum


    【解决方案1】:

    对于超过 1 个元素的比较,请使用 %in%,但 %in% 适用于 vector。所以,我们用lapply/sapply遍历列,然后在逻辑矩阵上做rowSums

    df$RSum <- rowSums(sapply(df[,2:5], `%in%`, 1:3))
    df$RSum
    #[1] 1 2 2 2 1
    

    如果值是数字,那么我们也可以使用&gt;&lt;

    df$RSum <- rowSums(df[, 2:5] >=1 & df[, 2:5] <=3)
    

    数据

    df <- structure(list(ID = c("a", "b", "c", "d", "e"), cond1 = c(2L, 
    7L, 4L, 7L, 6L), cond2 = c(7L, 2L, 3L, 3L, 7L), cond3 = c(6L, 
    3L, 1L, 3L, 7L), cond4 = c(6L, 6L, 4L, 6L, 3L)), 
    class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5"))
    

    【讨论】:

      【解决方案2】:

      我建议您解决数据的两个问题:

      1. 您的数据很宽,而不是长格式。如果你的数据是长格式的,你的分析会简单得多。绘图尤其如此。
      2. 每个条件的值都是因子。这使得进行比较变得更加困难,并且可能会导致一些难以发现的错误。如果您仔细查看@akrun 的答案,您会注意到这些值是整数(数字)。

      也就是说,我提出一个data.table 解决方案:

      # 1. load libraries and make df a data.table:
      library(data.table)
      setDT(df)
      
      # 2. make the wide table a long one
      melt(df, id.vars = "ID")
      
      # 3. with a long table, count the number of conditions that are in the 1:3 range for each ID. Notice I chained the first command with this second one:
      melt(df, id.vars = "ID")[, sum(value %in% 1:3), by = ID]
      

      产生结果:

         ID V1
      1:  a  1
      2:  b  2
      3:  c  2
      4:  d  2
      5:  e  1
      

      您只需要运行 1 和 3 下的命令(2 已链接到 3)。有关详细信息,请参阅?data.table

      您可以在wikipediaMike Wise's answer 中阅读有关宽与长的更多信息

      我使用的数据和@akrun一样:

      df <- structure(list(ID = c("a", "b", "c", "d", "e"),
                                cond1 = c(2L, 7L, 4L, 7L, 6L), 
                                cond2 = c(7L, 2L, 3L, 3L, 7L), 
                                cond3 = c(6L, 3L, 1L, 3L, 7L), 
                                cond4 = c(6L, 6L, 4L, 6L, 3L)), 
                     class = "data.frame", 
                     row.names = c("1", "2", "3", "4", "5"))
      

      【讨论】:

        猜你喜欢
        • 2020-05-22
        • 2014-08-01
        • 2019-05-19
        • 2022-11-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2017-09-06
        • 1970-01-01
        相关资源
        最近更新 更多