【问题标题】:Comparison of elements in column between groups组间列中元素的比较
【发布时间】:2017-01-20 23:11:49
【问题描述】:

This thread 包含有关如何获取组之间匹配字符串数的有用说明。但是,我想弄清楚每个类别的特定组有多少个字符串是唯一的。

例子:

Category      Group         Text_Strings 
1             A             string1
1             A             string2
1             B             string1
1             B             string2
1             B             string3

2             A             string1
2             A             string3
2             B             string3

3             A             string1
3             A             string2
3             A             string3
3             B             string4
3             B             string5

对于 B 组,该函数将返回类别 1 中的一个唯一字符串,类别 2 中没有唯一字符串,类别 3 中返回两个。

Category     Count
1            1
2            0
3            2 

对于 A 组,它将返回:

Category     Count
1            0
2            1
3            3 

按照其他线程上的建议,查找唯一字符串应该像这样简单:

df %>% 
  distinct %>%
  group_by(category) %>%
  summarise(uniques = sum(
    strings[group == 'A'] %!in% strings[group == 'B']))

...但我不知道如何一次只为一个类别计算差异。是否有捷径可寻?非常感谢您的帮助!

【问题讨论】:

  • 对于 A 组,它应该是 Count = [0, 1, 3]
  • 感谢您发现错字

标签: r


【解决方案1】:

这是一种使用bysapplyaggregate 的方法。使用by,我们按类别计算每个字符串还有多少其他字符串匹配。我们的计算是通过使用sapply 遍历Text_Strings 中的每个字符串来执行的。执行此操作后,我们将unlist 的结果和cbind 的结果转换为我们的原始数据dat。然后,我们对aggregate 执行一个简单的调用,以查看每个类别和组有多少OtherMatches == 0

dat <- cbind(dat, 
             'OtherMatches' = unlist(
               by(dat, dat$Category, function(x)
                 sapply(x$Text_Strings, 
                        FUN = function(y) sum(y == x$Text_Strings) - 1))))

dat2 <- aggregate(OtherMatches ~ Category + Group, data = dat, 
          FUN = function(x) sum(x == 0))

setNames(dat2, c('Category', 'Group', 'Count'))

  Category Group Count
1        1     A     0
2        2     A     1
3        3     A     3
4        1     B     1
5        2     B     0
6        3     B     2

另一种方式

这是另一种方式,再次使用 split-apply-combine 框架。这一次,我们将使用一点基础R 和一点dplyr。首先,我们通过Categorysplit 获取数据。然后,我们使用lapply 对拆分数据进行操作,使用cbind 添加一个使用sapply 计算的新列(如前所述)。我们使用unsplit 合并数据,然后我们使用group_by CategoryGroup,然后像以前一样使用summarise

library(dplyr)

split(dat, dat$Category) %>%   
  lapply(., FUN = function(x) 
    cbind(x, 
          'OtherMatches' = 
            sapply(x$Text_Strings, 
                   FUN = function(y) sum(y == x$Text_Strings) - 1))) %>%
  unsplit(dat$Category) %>%
  group_by(Category, Group) %>%
  summarise(Count = sum(OtherMatches == 0))

Source: local data frame [6 x 3]
Groups: Category [?]

  Category Group Count
     <int> <chr> <int>
1        1     A     0
2        1     B     1
3        2     A     1
4        2     B     0
5        3     A     3
6        3     B     2

数据

dat <- structure(list(
  Category = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), 
  Group = c("A", "A", "B", "B", "B", "A", "A", "B", "A", "A", "A", "B", "B"), 
  Text_Strings = c("string1", "string2", "string1", "string2", "string3",
                   "string1", "string3", "string3", "string1", "string2", 
                   "string3", "string4", "string5")), 
  .Names = c("Category", "Group", "Text_Strings"), class = "data.frame", 
  row.names = c(NA, -13L))

【讨论】:

  • 就像一个魅力,非常详细的解释!奇怪的是,每种方法都为我的实际数据集提供了不同的计数,但这只是我最终要弄清楚的一些东西。再次感谢!!
猜你喜欢
  • 2020-08-15
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2012-05-04
  • 2015-09-30
相关资源
最近更新 更多