【问题标题】:All possible combinations of variable R变量 R 的所有可能组合
【发布时间】:2021-05-19 04:06:19
【问题描述】:

我有一个数据框中几个主题的变量和值列表(见下文)。我正在使用 R/R Studio 并希望重新格式化数据以返回每个可能的变量组合,以及每个主题的相应值。实际的数据集比这大得多,因此任何以编程方式进行的方式都将不胜感激。

谢谢

我目前的数据

Variable Subject Value
  V1       A      1
  V1       B      0
  V1       C      1
  V2       A      0
  V2       B      1
  V2       C      0
  V3       A      1
  V3       B      1
  V3       C      1

我想要的输出

Subject Variable 1     Variable 2      Value 1      Value 2
   A        V1              V2            1            0
   A        V1              V3            1            1
   A        V2              V3            0            1   
   B        V1              V2            0            1
   B        V1              V3            0            1
   B        V2              V3            1            1
   C        V1              V2            1            0
   C        V1              V3            1            1
   C        V2              V3            0            1

【问题讨论】:

    标签: r dataframe combinations


    【解决方案1】:

    您可以使用combn 为每个Subject 创建VariableValue 列的所有可能组合。

    library(dplyr)
    library(tidyr)
    
    create_data_frame <- function(var, val) {
      tab1 <- setNames(data.frame(t(combn(var, 2))), paste0('Variable', 1:2))
      tab2 <- setNames(data.frame(t(combn(val, 2))), paste0('Value', 1:2))
      cbind(tab1, tab2)
    }
    
    df %>%
      group_by(Subject) %>%
      summarise(data = list(create_data_frame(Variable, Value))) %>%
      unnest(col = data)
    
    #  Subject Variable1 Variable2 Value1 Value2
    #  <chr>   <chr>     <chr>      <int>  <int>
    #1 A       V1        V2             1      0
    #2 A       V1        V3             1      1
    #3 A       V2        V3             0      1
    #4 B       V1        V2             0      1
    #5 B       V1        V3             0      1
    #6 B       V2        V3             1      1
    #7 C       V1        V2             1      0
    #8 C       V1        V3             1      1
    #9 C       V2        V3             0      1
    

    【讨论】:

      【解决方案2】:

      一个基本的 R 解决方案,在 R 版本 4.1.0 中使用原生管道

      df <- read.table(text = 'Variable Subject Value
        V1       A      1
        V1       B      0
        V1       C      1
        V2       A      0
        V2       B      1
        V2       C      0
        V3       A      1
        V3       B      1
        V3       C      1', header = T)
      
      
      t(combn(unique(df$Variable), 2)) |> as.data.frame() |>
        setNames(c('Variable1', 'Variable2')) |>
        merge(df, by.x = 'Variable1', by.y = 'Variable') |>
        merge(df, by.x = c('Variable2', 'Subject'), by.y = c('Variable', 'Subject'), suffixes = c('1', '2'))
      
      #>   Variable2 Subject Variable1 Value1 Value2
      #> 1        V2       A        V1      1      0
      #> 2        V2       B        V1      0      1
      #> 3        V2       C        V1      1      0
      #> 4        V3       A        V2      0      1
      #> 5        V3       A        V1      1      1
      #> 6        V3       B        V1      0      1
      #> 7        V3       B        V2      1      1
      #> 8        V3       C        V1      1      1
      #> 9        V3       C        V2      0      1
      

      用期望的方式检查它

      t(combn(unique(df$Variable), 2)) |> as.data.frame() |>
        setNames(c('Variable1', 'Variable2')) |>
        merge(df, by.x = 'Variable1', by.y = 'Variable') |>
        merge(df, by.x = c('Variable2', 'Subject'), by.y = c('Variable', 'Subject'), suffixes = c('1', '2')) -> df_new
      
      df_new[order(df_new$Subject),c(2,3,1,4,5)]  
      #>   Subject Variable1 Variable2 Value1 Value2
      #> 1       A        V1        V2      1      0
      #> 4       A        V2        V3      0      1
      #> 5       A        V1        V3      1      1
      #> 2       B        V1        V2      0      1
      #> 6       B        V1        V3      0      1
      #> 7       B        V2        V3      1      1
      #> 3       C        V1        V2      1      0
      #> 8       C        V1        V3      1      1
      #> 9       C        V2        V3      0      1
      

      reprex package (v2.0.0) 于 2021 年 5 月 19 日创建

      【讨论】:

      • 也可以,但我不能将两个答案都标记为正确的。但无论如何都赞成。也谢谢你的帮助:)
      • @thehand0 很高兴能帮上忙。接受任何答案纯粹是个人选择,所以没有问题。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多