【问题标题】:mutate several columns into a function creates a lists for each component inside the resulted column in dplyr将几列变异为一个函数为 dplyr 中结果列内的每个组件创建一个列表
【发布时间】:2019-02-17 08:39:41
【问题描述】:

我有一个来自包(tidycensus)的函数,我想通过管道使用它并进行变异。我创建了这个简单的模型来展示这种情况。

library(tidycensus)

tt <- as_data_frame(matrix(1:36, ncol = 6))
colnames(tt) <- c("A", "B", "C", "D", "E", "F")
tt2 <- tt %>% mutate(moe=moe_prop(.[,"A"],.[,"C"], .[,"D"],.[,"B"]))

最终结果将结果包装到列表中(都与每个列表中的计算值相等)并将它们放在 moe 列的每个位置,如下所示。显然,我想要一个填充列 moe

的向量
> tt2
# A tibble: 6 x 7
      A     B     C     D     E     F    moe      
    <int> <int> <int> <int> <int> <int> <list>   
1     1     7    13    19    25    31 <dbl [6]>
2     2     8    14    20    26    32 <dbl [6]>
3     3     9    15    21    27    33 <dbl [6]>
4     4    10    16    22    28    34 <dbl [6]>
5     5    11    17    23    29    35 <dbl [6]>
6     6    12    18    24    30    36 <dbl [6]>

我知道使用[,"Column_name] 格式返回列表。因此,我尝试在函数的每个输入变量之前添加as.vector。还是一样的结果。我想知道我在这里缺少什么。

【问题讨论】:

    标签: r dplyr tidyverse tidycensus


    【解决方案1】:

    基于输入数据集,假设我们每行只需要一个值,通过对每一行执行moe_prop,将列名转换为符号,然后进行评估(!!!

    tt %>% 
      mutate(moe = moe_prop(!!! rlang::syms(names(.)[c(1, 3, 4, 2)])))
    # A tibble: 6 x 7
    #      A     B     C     D     E     F   moe
    #  <int> <int> <int> <int> <int> <int> <dbl>
    #1     1     7    13    19    25    31  1.46
    #2     2     8    14    20    26    32  1.43
    #3     3     9    15    21    27    33  1.39
    #4     4    10    16    22    28    34  1.37
    #5     5    11    17    23    29    35  1.34
    #6     6    12    18    24    30    36  1.31
    

    类似调用

    tt %>%
       mutate(moe = moe_prop(!!! rlang::syms(c("A", "C", "D", "B"))))
    

    或者做一个 rowwise() 操作

    tt %>%
        rowwise %>% 
        mutate(moe = moe_prop(A, C, D, B))
    

    通过单独检查行值

    moe_prop(1, 13, 19, 7)
    #[1] 1.460951
    
    moe_prop(2, 14, 20, 8)
    #[1] 1.426237
    

    【讨论】:

    • 非常感谢。这真的很棒。但是如果我想直接给出列的名称怎么办?在我的实际问题中,输入列的名称在不同的运行中是不同的,因为它们的数字也可能不同,因此列索引可能会改变,所以每次我都有一组列名。
    • @BobbyF,添加了另一个会有点慢的选项。在第一种情况下,我们直接转换列名。唯一的问题是,我更容易做到这一点。相反,您可以这样做 tt %&gt;% mutate(moe = moe_prop(!!! rlang::syms(c("A", "C", "D", "B")))
    • 哦。这是正确的!!我认为基本的 R。完全忘记了。非常感谢。
    【解决方案2】:

    你可以使用unnest()

    library(dplyr)
    library(tidyr)
    library(tidycensus)
    
    tt <- as_data_frame(matrix(1:36, ncol = 6))
    colnames(tt) <- c("A", "B", "C", "D", "E", "F")
    tt2 <- tt %>% 
      mutate(moe = moe_prop(.[, "A"], .[, "C"], .[, "D"], .[, "B"]))
    
    tt2 %>%
      unnest()
    #> # A tibble: 36 x 7
    #>        A     B     C     D     E     F   moe
    #>    <int> <int> <int> <int> <int> <int> <dbl>
    #>  1     1     7    13    19    25    31  1.46
    #>  2     1     7    13    19    25    31  1.43
    #>  3     1     7    13    19    25    31  1.39
    #>  4     1     7    13    19    25    31  1.37
    #>  5     1     7    13    19    25    31  1.34
    #>  6     1     7    13    19    25    31  1.31
    #>  7     2     8    14    20    26    32  1.46
    #>  8     2     8    14    20    26    32  1.43
    #>  9     2     8    14    20    26    32  1.39
    #> 10     2     8    14    20    26    32  1.37
    #> # ... with 26 more rows
    

    reprex package (v0.2.0.9000) 于 2018 年 9 月 12 日创建。

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2018-08-05
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2021-11-15
      • 2015-01-31
      相关资源
      最近更新 更多