【问题标题】:How can I count the number of times one item has been grouped together with another in R?如何计算一个项目在 R 中与另一个项目组合在一起的次数?
【发布时间】:2018-07-06 08:31:37
【问题描述】:

我有一张桌子(示例):

 Group  |  Country
-------------------
 Group1      SE
 Group1      DE  
 Group2      SE   
 Group2      DE
 Group2      FI
 Group3      SE
 Group3      FI

我正在尝试将其转换为:

 Country 1 | Country 2 | Count
-------------------------------
    SE          DE         2
    SE          FI         2
    FI          DE         1

我尝试使用 dplyr 的计数 group_by 总结,但我似乎无法理解它。相反,我得到了一个表格,其中每个国家/地区作为列,每个组作为行,如果国家/地区在组中,则单元格中的 1 或 0。

【问题讨论】:

标签: r dplyr


【解决方案1】:

我们可以使用base R 方法,使用table 获取频率,执行crossprod,将对角线和下三角形元素设置为NA,并在转换为data.frame 后删除NA

m1 <- crossprod(table(df1))
m1[lower.tri(m1, diag = TRUE)] <- NA
subset(as.data.frame.table(m1), !is.na(Freq))
#    Country Country.1 Freq
#4      DE        FI    1
#7      DE        SE    2
#8      FI        SE    2

数据

df1 <- structure(list(Group = c("Group1", "Group1", "Group2", "Group2", 
"Group2", "Group3", "Group3"), Country = c("SE", "DE", "SE", 
"DE", "FI", "SE", "FI")), .Names = c("Group", "Country"),
 class = "data.frame", row.names = c(NA, -7L))

【讨论】:

    【解决方案2】:

    这是使用combn 的替代tidyverse 方法

    library(tidyverse)
    df %>%
        group_by(Group) %>%
        summarise(cmbn = list(apply(combn(Country, 2), 2, function(x)
            paste(sort(x), collapse = "_")))) %>%
        unnest() %>%
        select(-Group) %>%
        separate(cmbn, into = c("Country 1", "Country 2"), sep = "_") %>%
        count(`Country 1`, `Country 2`)
    ## A tibble: 3 x 3
    #  `Country 1` `Country 2`     n
    #  <chr>       <chr>       <int>
    #1 DE          FI              1
    #2 DE          SE              2
    #3 FI          SE              2
    

    样本数据

    df <- read.table(text =
        "Group    Country
     Group1      SE
     Group1      DE
     Group2      SE
     Group2      DE
     Group2      FI
     Group3      SE
     Group3      FI", header = T, stringsAsFactors = F)
    

    【讨论】:

    • 您不需要apply,因为combnFUN 参数,即df %&gt;% group_by(Group) %&gt;% summarise(cmbn = list(combn(Country, 2, FUN = function(x) paste(sort(x), collapse="_"))))
    【解决方案3】:

    另一种dplyr 方法将函数应用于每个Country 值组合

    df = read.table(text = "
    Group Country
    Group1      SE
    Group1      DE  
    Group2      SE   
    Group2      DE
    Group2      FI
    Group3      SE
    Group3      FI
    ", header=T, stringsAsFactors=F)
    
    library(dplyr)
    
    # function that takes 2 Country values and returns the number of common groups they have
    f = function(x,y) { 
      df %>% 
        filter(Country %in% c(x,y)) %>% 
        distinct() %>%
        count(Group) %>%
        filter(n > 1) %>%
        nrow() 
    }
    
    # vectorising the function
    f = Vectorize(f)
    
    # applying the function to each Country value combination
    data.frame(t(combn(unique(df$Country), 2)), stringsAsFactors = F) %>%
      mutate(NumGroups = f(X1, X2))
    
    #   X1 X2 NumGroups
    # 1 SE DE         2
    # 2 SE FI         2
    # 3 DE FI         1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2022-01-23
      • 2012-07-08
      • 1970-01-01
      相关资源
      最近更新 更多