【问题标题】:Count unique instances in a column given the names of two other columns给定其他两列的名称,计算一列中的唯一实例
【发布时间】:2018-02-08 04:36:49
【问题描述】:

我有下表(称为火车)(实际上要大得多)

UNSPSC adaptor alert bact blood collection packet patient ultrasoft whit
 514415       1     0    1     0          0      0       0         1    0
 514415       0     0    0     1          1      0       0         1    0
 514415       0     0    1     0          0      0       0         1    0
 514415       0     0    0     0          0      0       0         1    0
 514415       1     0    1     0          0      0       0         1    0
 514415       0     0    0     0          0      0       0         1    0
 422018       1     0    1     0          0      0       0         1    0
 422018       0     0    0     0          0      0       0         1    0
 422018       0     0    0     1          0      0       0         1    0
 411011       0     0    0     0          0      0       0         1    0

还有下表,我称之为关联:

 lhd     rhs
blood   collection
adaptor bact
[...]

我想计算 lhs 和 rhs 关联表中每条记录的值等于 1 的每列唯一 UNSPSC 的数量。 喜欢:

血液收集 1 适配器 2

这段代码一次只做一个学期。

apply(train[,-1], 2, function(x) length(unique(substr(train$UNSPSC,1,4)[x == 1])))

【问题讨论】:

    标签: r count unique apply


    【解决方案1】:

    您可以迭代 associations 并使用 subsetx 第 1 行和第 2 行等于 1 的子集)、uniquelength 函数,而不是迭代 trains
    使用get 函数调用来自行x 的列。

    train$lhd <- 1
    train$rhs <- 1
    apply(associations, 1, function(x)
        length(unique(subset(train, get(x[1]) == 1 & get(x[2]) == 1)$UNSPSC))
    )
    # [1] 3 1 2
    

    数据(train):

    structure(list(UNSPSC = c(514415L, 514415L, 514415L, 514415L, 
    514415L, 514415L, 422018L, 422018L, 422018L, 411011L), adaptor = c(1L, 
    0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L), alert = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L), bact = c(1L, 0L, 1L, 0L, 1L, 0L, 1L, 
    0L, 0L, 0L), blood = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L
    ), collection = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), packet = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), patient = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), ultrasoft = c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), whit = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), lhd = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), rhs = c(1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("UNSPSC", "adaptor", "alert", 
    "bact", "blood", "collection", "packet", "patient", "ultrasoft", 
    "whit", "lhd", "rhs"), row.names = c(NA, -10L), class = "data.frame")
    

    数据(associations):

    structure(list(V1 = c("lhd", "blood", "adaptor"), V2 = c("rhs", 
    "collection", "bact")), .Names = c("V1", "V2"), row.names = c(NA, 
    -3L), class = "data.frame")
    

    【讨论】:

      【解决方案2】:

      tidyverse 类似的选项是(来自@PoGibas 帖子的数据)将pmap 应用于“关联”数据以循环遍历列,filter 列均为1 的“火车”, pull 'UNSCPSC' 列并获取 unique 元素中的 length (n_distinct)

      library(tidyverse)
      pmap_int(associations, ~ train %>% 
                                 filter(!! rlang::sym(.x) == 1, !! rlang::sym(.y) == 1) %>% 
                                 pull(UNSPSC) %>% 
                                 n_distinct)
      #[1] 3 1 2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        • 1970-01-01
        相关资源
        最近更新 更多