【问题标题】:Sum a variable in a grouped dataframe only once for each unique combination of two other variables with dplyr对于具有 dplyr 的其他两个变量的每个唯一组合,仅对分组数据框中的变量求和一次
【发布时间】:2019-09-10 03:38:00
【问题描述】:

我有一张长桌,上面有areacluster 的重复组合。

counts <-  tibble::tribble(
         ~age,         ~area,          ~cluster, ~norm.to.area,
      "gw_25",   "cingulate",       "cluster_1",          0.03,
      "gw_20",   "cingulate",       "cluster_1",          0.03,
      "gw_18", "hippocampus",       "cluster_1",          0.02,
      "gw_25",      "insula",       "cluster_1",          0.01,
      "gw_20",       "motor",       "cluster_1",          0.01,
      "gw_22",       "motor",       "cluster_1",          0.01,
      "gw_25",       "motor",       "cluster_1",          0.01,
      "gw_14",       "motor",       "cluster_1",          0.01,
      "gw_18",       "motor",       "cluster_1",          0.01,
      "gw_19",       "motor",       "cluster_1",          0.01,
      "gw_17",       "motor",       "cluster_1",          0.01,
      "gw_20",   "occipital",       "cluster_1",          0.01,
      "gw_17",   "occipital",       "cluster_1",          0.01,
      "gw_18",   "occipital",       "cluster_1",          0.01,
      "gw_19",   "occipital",       "cluster_1",          0.01,
      "gw_22",   "occipital",       "cluster_1",          0.01,
      "gw_14",   "occipital",       "cluster_1",          0.01,
      "gw_22",    "parietal",       "cluster_1",             0,
      "gw_25",    "parietal",       "cluster_1",             0,
      "gw_17",    "parietal",       "cluster_1",             0,
      "gw_19",    "parietal",       "cluster_1",             0,
      "gw_20",    "parietal",       "cluster_1",             0,
      "gw_20",         "PFC",       "cluster_1",          0.01,
      "gw_22",         "PFC",       "cluster_1",          0.01,
      "gw_25",         "PFC",       "cluster_1",          0.01
      )

我想创建一个新变量sum.norm.to.area,它是每个clusternorm.to.area 的总和,对于area / subcluster.merge 的每个组合只使用一次norm.to.area 的值。

我尝试过group_bycluster,但这会在出现给定组合时多次求和。

counts %&gt;% group_by(cluster) %&gt;% mutate(sum.norm.to.area = sum(norm.to.area)

感谢您的建议。

更新 1:

尝试使用以下建议的汇总,但发生了同样的事情(当然,除了没有添加为新列):

&gt; counts %&gt;% group_by(subcluster.merge, area) %&gt;% summarize(sum(norm.to.area))

    tibble::tribble(
      ~cluster .       ,           ~area, ~sum.norm.to.area.,
            "cluster_1",           "PFC",               0.06,
            "cluster_1", "somatosensory",               0.05,
            "cluster_1",         "motor",               0.07,
            "cluster_1",      "parietal",                  0,
            "cluster_1",      "temporal",               0.03,
            "cluster_1",     "occipital",               0.06,
            "cluster_1",   "hippocampus",               0.02,
            "cluster_1",        "insula",               0.01,
            "cluster_1",     "cingulate",               0.06,
        "cluster_10-34",           "PFC",               0.42,
        "cluster_10-34", "somatosensory",               0.35,
        "cluster_10-34",         "motor",               0.48,
        "cluster_10-34",      "parietal",               0.36,
        "cluster_10-34",      "temporal",               0.28,
        "cluster_10-34",     "occipital",                0.4,
        "cluster_10-34",   "hippocampus",               0.12,
        "cluster_10-34",        "insula",                  0,
        "cluster_10-34",     "cingulate",                  0,
           "cluster_11",           "PFC",               0.18,
           "cluster_11", "somatosensory",               0.15,
           "cluster_11",         "motor",               0.14,
           "cluster_11",      "parietal",               0.12,
           "cluster_11",      "temporal",               0.04,
           "cluster_11",     "occipital",               0.18,
           "cluster_11",   "hippocampus",               0.02
      )

更新 2

这是我想要的输出,但我得到它的方式太复杂了。我想找到一种使用 mutate 而不必使用join 的更简单方法。

 > tmp <- counts %>% distinct(area, cluster, .keep_all = TRUE) %>%
 add_count(cluster, wt = norm.to.area, name = "sum.norm.to.area")

counts %>% left_join(tmp, by = c("cluster", "area"))

期望的输出: sum.norm.to.area 是对areacluster 的所有唯一组合添加norm.to.area(仅一次)的结果:

     tibble::tribble(
         ~age,           ~area,          ~cluster, ~norm.to.area, ~sum.norm.to.area,
      "gw_25",     "cingulate",       "cluster_1",          0.03,              0.11,
      "gw_20",     "cingulate",       "cluster_1",          0.03,              0.11,
      "gw_18",   "hippocampus",       "cluster_1",          0.02,              0.11,
      "gw_25",        "insula",       "cluster_1",          0.01,              0.11,
      "gw_20",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_22",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_25",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_14",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_18",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_19",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_17",         "motor",       "cluster_1",          0.01,              0.11,
      "gw_20",     "occipital",       "cluster_1",          0.01,              0.11,
      "gw_17",     "occipital",       "cluster_1",          0.01,              0.11,
      "gw_18",     "occipital",       "cluster_1",          0.01,              0.11,
      "gw_19",     "occipital",       "cluster_1",          0.01,              0.11,
      "gw_22",     "occipital",       "cluster_1",          0.01,              0.11,
      "gw_14",     "occipital",       "cluster_1",          0.01,              0.11,
      "gw_22",      "parietal",       "cluster_1",             0,              0.11,
      "gw_25",      "parietal",       "cluster_1",             0,              0.11,
      "gw_17",      "parietal",       "cluster_1",             0,              0.11,
      "gw_19",      "parietal",       "cluster_1",             0,              0.11,
      "gw_20",      "parietal",       "cluster_1",             0,              0.11,
      "gw_20",           "PFC",       "cluster_1",          0.01,              0.11,
      "gw_22",           "PFC",       "cluster_1",          0.01,              0.11,
      "gw_25",           "PFC",       "cluster_1",          0.01,              0.11,
      "gw_18",           "PFC",       "cluster_1",          0.01,              0.11,
      "gw_19",           "PFC",       "cluster_1",          0.01,              0.11,
      "gw_17",           "PFC",       "cluster_1",          0.01,              0.11,
      "gw_22", "somatosensory",       "cluster_1",          0.01,              0.11,
      "gw_20", "somatosensory",       "cluster_1",          0.01,              0.11,
      "gw_25", "somatosensory",       "cluster_1",          0.01,              0.11,
      "gw_18", "somatosensory",       "cluster_1",          0.01,              0.11,
      "gw_19", "somatosensory",       "cluster_1",          0.01,              0.11,
      "gw_25",      "temporal",       "cluster_1",          0.01,              0.11,
      "gw_19",      "temporal",       "cluster_1",          0.01,              0.11,
      "gw_20",      "temporal",       "cluster_1",          0.01,              0.11
      )

【问题讨论】:

  • 为什么所有sum.norm.to.area 都是一样的?你是怎么计算的?你能用一个例子解释一个计算吗?
  • 对于 cluster_1 列,它是 norm.to.area 对于 area 的所有唯一值的总和。即 0.03 + 0.02 + 0.01 + 0.01 + 0 + 0.01 .... 。因此,您只需将每个区域添加一次。
  • 那么总和不是 0.09 吗? 0.03 + 0.02 + 0.01 + 0.01 + 0.01 + 0.00 + 0.01 = 0.09 ?
  • 是的,这只是表的head。更新它以显示所有 cluster_1 行。
  • 我怀疑有比更新 2 更简单的方法。

标签: r group-by tidyverse dplyr


【解决方案1】:

我认为您正在寻找summarize()而不是mutate

counts %>% group_by(cluster, area) %>% summarize(sum.norm.to.area = sum(norm.to.area))

【讨论】:

  • 谢谢!但是,这仍然给出了具有给定组合的 all 行的总和。查看更新。
  • 试试这个(我假设你的数据中有超过 1 个集群)计数 %>% select(-age) %>% unique() %>% group_by(cluster) %>% summarise (sum.norm.to.area = sum(norm.to.area))。独特的功能将摆脱您的重复行,因为在您的问题的情况下,年龄不再是一个特别重要的变量,对吗?
  • 我想保留表格中的所有变量,包括年龄。
  • 所以你会期望 sum.norm.to.area 也是一个重复/多次重复的序列?
  • 是的。出于ggplot 的目的,它必须是这样的。我在上面添加了所需的输出。
【解决方案2】:

使用dplyr,我们可以在group_by clustersum 中仅使用每个area 中的唯一值。

library(dplyr)

counts %>%
   group_by(cluster) %>%
   mutate(sum.norm = sum(norm.to.area[!duplicated(area)]))


#   age   area        cluster   norm.to.area sum.norm
#   <chr> <chr>       <chr>            <dbl>    <dbl>
# 1 gw_25 cingulate   cluster_1         0.03     0.09
# 2 gw_20 cingulate   cluster_1         0.03     0.09
# 3 gw_18 hippocampus cluster_1         0.02     0.09
# 4 gw_25 insula      cluster_1         0.01     0.09
# 5 gw_20 motor       cluster_1         0.01     0.09
# 6 gw_22 motor       cluster_1         0.01     0.09
# 7 gw_25 motor       cluster_1         0.01     0.09
# 8 gw_14 motor       cluster_1         0.01     0.09
# 9 gw_18 motor       cluster_1         0.01     0.09
#10 gw_19 motor       cluster_1         0.01     0.09
# … with 15 more rows

【讨论】:

  • 太棒了。谢谢!
  • 否决票背后的任何原因?很乐意纠正它。
  • 不确定这是从哪里来的——我赞成你的回答。我的问题也被否决了.. :/
猜你喜欢
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多