【问题标题】:How can I convert a two-column "count" matrix to a binary vector in R? [duplicate]如何将两列“计数”矩阵转换为 R 中的二进制向量? [复制]
【发布时间】:2015-03-03 16:00:36
【问题描述】:

如何将具有两列计数矩阵的数据帧转换为 R 中具有单个二进制向量的数据帧?例如,我有一个这样的数据框,其中 id 是主题的 id,s 和 f 是该主题的“成功”和“失败”的数量,x 是描述该主题某些特征的第三个变量.

id s f x
1  0 3 A
2  2 1 A
3  1 2 B

我希望将此数据框转换为:

id n x
1  f A
1  f A
1  f A
2  s A
2  s A
2  f A
3  s B
3  f B
3  f B

其中第 n 列表示每次试验是成功 (s) 还是失败 (f)。

我确定我可以编写一个函数来执行此操作,但我想知道是否有预制解决方案。

【问题讨论】:

    标签: r reshape


    【解决方案1】:

    这是使用tidyrsplitstackshape 包的一种方法。您使用gather 重塑您的数据。然后,您可以在splitstackshape 包中使用expandRows。您要求 R 按值列中的数字重复每一行。出于显示目的,我使用了dplyr 包中的arrange()。但是,这部分是可选的。

    library(tidyr)
    library(splitstackshape)
    library(dplyr)
    
    gather(mydf, variable, value, -id, -x) %>%
    expandRows("value") %>%
    arrange(id, x)
    
    
    #  id x variable
    #1  1 A        f
    #2  1 A        f
    #3  1 A        f
    #4  2 A        s
    #5  2 A        s
    #6  2 A        f
    #7  3 B        s
    #8  3 B        f
    #9  3 B        f
    

    【讨论】:

      【解决方案2】:
        dd <- read.table(text="id s f x
          1  0 3 A
          2  2 1 A
          3  1 2 B",
          header=TRUE)
      
       with(dd,data.frame(
               id=rep(id,s+f),
               n=rep(rep(c("s","f"),nrow(dd)),c(rbind(s,f))),
               x=rep(x,s+f)))
      

      【讨论】:

      • 太棒了。奇迹般有效。请参阅下面使用此代码的函数,该代码适用于任何数据框,具有任意数量的列。希望对您有所帮助!
      • 相反呢?
      • @Bakaburg,请继续问一个新问题。某些版本的table 加上as.data.frame 加上cbind 应该可以做到。
      • 我找到了一种方法......在一行中cbind(as.data.frame(table(df[2:(length(df))])), Success = as.data.frame(table(df[df[1] == 'y', 2:(length(df))]))$Freq)
      • 您仍然可以将此作为问题发布,自己回答,看看是否有人提出了更好/更快/更优雅的方法
      【解决方案3】:

      使用上面 Ben Bolker 的出色回答,我创建了一个简短的函数,它将对任何包含成功计数的列、失败计数的列以及包含每行信息的任意数量的附加列的任何数据框执行此操作 (主题)。请参阅下面的示例。

      #####################################################################
      ### cnt2bin (count to binary) takes a data frame with 2-column ######
      ### "count" response variable of successes and failures and    ######
      ### converts it to long format, with one column showing        ######
      ### 0s and 1s for failures and successes.                      ######
      ### data is data frame with 2-column response variable         ######
      ### suc and fail are character expressions for columns         ######
      ### containing counts of successes and failures respectively   ######
      #####################################################################
      
      cnt2bin <- function(data, suc, fail) {
      
        xvars <- names(data)[names(data)!=suc & names(data)!=fail]
        list <- lapply(xvars, function(z) with(data, rep(get(z), get(suc)+get(fail))))
        names(list) <- xvars
        df <- as.data.frame(list)
        with(data,data.frame(bin=rep(rep(c(1,0),nrow(data)),c(rbind(get(suc),get(fail)))),
                             df))
      }
      

      示例,其中 id 是主题 ID,s 和 f 是计算每个主题的成功和失败的列,x 和 y 是描述每个主题的属性的变量,将被扩展并添加到最终数据框中。

      dd <- read.table(text="id s f x y
                             1  0 3 A A
                             2  2 1 A B
                             3  1 2 B B",
                        header=TRUE)
      
      cnt2bin(dd, "s", "f")
      

      【讨论】:

      • 很高兴看到您彻底评论您的功能。如果您想养成一个对制作包有很大帮助的好习惯,您可以使用Roxygen2 syntax评论函数。
      • 谢谢,我会调查的。干杯。
      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 2021-09-09
      • 2015-05-13
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多