【问题标题】:Summarise using multiple functions with dplyr across()使用 dplyr cross() 总结使用多个函数
【发布时间】:2021-02-18 19:54:44
【问题描述】:

我有数据,其中一个 id 变量应该标识一个独特的观察。但是,有些 id 是重复的。我想通过按 id 分组然后计算每个变量的不一致响应的比例来了解哪些测量值正在推动这种重复。

下面是我的意思的一个例子:

require(tidyverse)

df <- tibble(id = c(1,1,2,3,4,4,4),
             col1 = c('a','a','b','b','c','c','c'), # perfectly consistent
             col2 = c('a','b','b','b','c','c','c'), # id 1 is inconsistent - proportion inconsistent = 0.25
             col3 = c('a','a','b','b','a','b','c'), # id 4 is inconsistent - proportion inconsistent = 0.25
             col4 = c('a','b','b','b','b','b','c') # id 1 and 4 are inconsistent - proportion inconsistent = 0.5
             )

我可以使用 group_by()、cross() 和 n_distinct() 来测试 id 中的不一致响应,如下所示:

# count the number of distinct responses for each id in each column
# if the value is equal to 1, it means that all responses were consistent
df <- df %>% 
  group_by(id) %>% 
  mutate(across(.cols = c(col1:col4), ~n_distinct(.), .names = '{.col}_distinct')) %>% 
  ungroup()

为简单起见,我现在可以为每个 id 取一行:

# take one row for each test (so we aren't counting duplicates twice)
df <- distinct(df, across(c(id, contains('distinct'))))

现在我想计算每个变量包含不一致响应的 id 的比例。我想做类似以下的事情:

consistency <- df %>% 
  summarise(across(contains('distinct'), ~sum(.>1) / n(.)))

但这会产生以下错误,我无法解释:

Error: Problem with `summarise()` input `..1`.
x unused argument (.)
ℹ Input `..1` is `across(contains("distinct"), ~sum(. > 1)/n(.))`.

我可以通过以下方式得到我想要的答案:

# calculate consistency for each column by finding the number of distinct values greater 
# than 1 and dividing by total rows
# first get the number of distinct values
n_inconsistent <- df %>% 
  summarise(across(.cols = contains('distinct'), ~sum(.>1)))

# next get the number of rows
n_total <- nrow(df)

# calculate the proportion of tests that have more than one value for each column
consistency <- n_inconsistent %>% 
  mutate(across(contains('distinct'), ~./n_total))

但这涉及到中间变量,感觉不雅。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    您可以通过以下方式进行:

    library(dplyr)
    
    df %>%
      group_by(id) %>%
      summarise(across(starts_with('col'), n_distinct)) %>%
      summarise(across(starts_with('col'), ~mean(. > 1), .names = '{col}_distinct'))
    
    #  col1_distinct col2_distinct col3_distinct col4_distinct
    #          <dbl>         <dbl>         <dbl>         <dbl>
    #1             0          0.25          0.25           0.5
    

    首先我们计算每个id 中每列中唯一值的数量,然后计算每列中大于 1 的值的比例。

    【讨论】:

    • 谢谢,这是一个很好的解决方案。不过我想知道,你能告诉我为什么我的解决方案涉及 ~sum(.>1) / n(.) 不起作用吗?
    • n() 中不需要.~sum(.&gt;1) / n() 有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2019-07-16
    • 2014-12-02
    • 1970-01-01
    相关资源
    最近更新 更多