【问题标题】:Create multiple dataframes based on combinations of select columns根据选择列的组合创建多个数据框
【发布时间】:2019-10-09 22:42:01
【问题描述】:

我有一个大型编码器数据框,它们对感兴趣的构造进行评级。最终,我想在每个编码器对上运行 kappa 可靠性(然后采用加权平均值)。首先,我需要找到一种方法来获取下面的单个数据帧(test_data)并为每个编码器组合(pair1、pair2、pair3 等)创建多个数据帧,我最终将通过一个更大的函数来评估 kappa 可靠性。

test_data <- data.frame(turn = c("s1: text string", "s2: text string" , "s1: text string", "s2: text string", "s1: text string", "s2: text string", "s1: text string"),
                        id = c(12, 12, 12, 15, 15, 17, 17),
                        coder1_1 = c("high", "low", "med", "high", "high", "high", "low"),
                        coder2_1 = c("high", "low", "med", "high", "med", "high", "low"),
                        coder3_1 = c("med", "med", "med", "high", "low", "high", "med"),
                        coder4_1 = c("high", "low", "med", "high", "med", "high", "low")
)

我想为每个编码器对创建 6 个单独的数据帧,同时保留每个数据帧中的前两列(turnid)。

例如,数据框“pair1”将是:

             turn id coder1_1 coder2_1 
1 s1: text string 12     high     high
2 s2: text string 12      low      low
3 s1: text string 12      med      med
4 s2: text string 15     high     high
5 s1: text string 15     high      med
6 s2: text string 17     high     high
7 s1: text string 17      low      low

下一个数据帧将是“pair2”:

             turn id coder1_1 coder3_1 
1 s1: text string 12     high      med
2 s2: text string 12      low      med
3 s1: text string 12      med      med
4 s2: text string 15     high     high
5 s1: text string 15     high      low
6 s2: text string 17     high     high
7 s1: text string 17      low      med

等...贯穿四个编码器的所有二元比较(总共 6 个)。

我在combn(names(test_data[,c(3:6)]),2,simplify=FALSE) 上取得的成功有限,因为这只是创建了一个列名列表并且没有保留turnid

非常感谢任何帮助。

【问题讨论】:

    标签: r dplyr tidyr


    【解决方案1】:

    我们可以在数据本身上使用combn,然后通过使用FUNcbind前两列

    combn(test_data[3:6], 2, simplify = FALSE, 
            FUN = function(x) cbind(test_data[1:2], x))
    #[[1]]
    #             turn id coder1_1 coder2_1
    #1 s1: text string 12     high     high
    #2 s2: text string 12      low      low
    #3 s1: text string 12      med      med
    #4 s2: text string 15     high     high
    #5 s1: text string 15     high      med
    #6 s2: text string 17     high     high
    #7 s1: text string 17      low      low
    
    #[[2]]
    #             turn id coder1_1 coder3_1
    #1 s1: text string 12     high      med
    #2 s2: text string 12      low      med
    #3 s1: text string 12      med      med
    #4 s2: text string 15     high     high
    #5 s1: text string 15     high      low
    #6 s2: text string 17     high     high
    #7 s1: text string 17      low      med
    
    #[[3]]
    #             turn id coder1_1 coder4_1
    #1 s1: text string 12     high     high
    #2 s2: text string 12      low      low
    #3 s1: text string 12      med      med
    #4 s2: text string 15     high     high
    #5 s1: text string 15     high      med
    #6 s2: text string 17     high     high
    #7 s1: text string 17      low      low
    
    #...
    

    【讨论】:

    • 谢谢。一个额外的问题。如果我想按列表删除 6 个数据帧中的每一个上的缺失值,我将如何将其包含在上面的函数中?我尝试了 drop_na(x),但没有成功。
    • @bpace 如果打算删除具有任何 NA 的行,则使用 na.omit combn(test_data[3:6], 2, simplify = FALSE, FUN = function(x) na.omit(cbind(test_data[1:2], x))) 包装
    【解决方案2】:

    另一种选择:

    pairs <- combn(grep("coder", colnames(test_data), value = TRUE), 2, simplify = FALSE)
    str(pairs)
    # List of 6
    #  $ : chr [1:2] "coder1_1" "coder2_1"
    #  $ : chr [1:2] "coder1_1" "coder3_1"
    #  $ : chr [1:2] "coder1_1" "coder4_1"
    #  $ : chr [1:2] "coder2_1" "coder3_1"
    #  $ : chr [1:2] "coder2_1" "coder4_1"
    #  $ : chr [1:2] "coder3_1" "coder4_1"
    
    lapply(pairs, function(p) test_data[,c("turn", "id", p)])
    # [[1]]
    #              turn id coder1_1 coder2_1
    # 1 s1: text string 12     high     high
    # 2 s2: text string 12      low      low
    # 3 s1: text string 12      med      med
    # 4 s2: text string 15     high     high
    # 5 s1: text string 15     high      med
    # 6 s2: text string 17     high     high
    # 7 s1: text string 17      low      low
    # [[2]]
    #              turn id coder1_1 coder3_1
    # 1 s1: text string 12     high      med
    # 2 s2: text string 12      low      med
    ### ...
    

    或者在@akrun 的回答中使用list 技巧上的combn

    lapply(combn(test_data[,3:6], 2, simplify = FALSE),
           cbind, test_data[,1:2])
    

    【讨论】:

    • 你的第二个答案类似于combn(test_data[,3:6], 2, cbind, FALSE, test_data[,1:2])我不认为你需要lapply
    • 好点...我通常不会在combn 中使用FUN=,所以它不是第一个想到的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多