使用dplyr将R中的元组列表除以R中的元组列表

时间:2018-01-24 15:55:44

标签: r dplyr tibble

假设我有一个嵌套的tibble,格式如下:

# A tibble: 3 x 3
AccountNumber Tibble1          Tibble2         
         <int> <list>           <list>          
1             1 <tibble [1 x 3]> <tibble [1 x 3]>
2             2 <tibble [1 x 3]> <tibble [1 x 3]>
3             3 <tibble [1 x 3]> <tibble [1 x 3]>

这可以通过以下代码生成:

library(tidyverse)

tibble1 <- tibble(AccountNumber = 1:3, A_1 = 1, B_1 = 2, C_1 = 3) %>%
  group_by(AccountNumber) %>%
  nest(.key = "Tibble1")

tibble2 <- tibble(AccountNumber = 1:3, A_2 = 4, B_2 = 5, C_2 = 6) %>%
  group_by(AccountNumber) %>%
  nest(.key = "Tibble2")

tibble_joined <- left_join(tibble1, tibble2, by = "AccountNumber")
  • 如何通过将Tibble1除以Tibble 2来创建第三个元组列表?

基本上我想要以下格式:

# A tibble: 3 x 3
AccountNumber Tibble1          Tibble2            Tibble3(Tibble2 / Tibble1)
         <int> <list>           <list>            <list>
1             1 <tibble [1 x 3]> <tibble [1 x 3]> <tibble [1 x 3]>
2             2 <tibble [1 x 3]> <tibble [1 x 3]> <tibble [1 x 3]>
3             3 <tibble [1 x 3]> <tibble [1 x 3]> <tibble [1 x 3]>

......其中Tibble3只是Tibble 2与Tibble 1之比:

  • 的每一栏
  • 每个帐号

到目前为止,我的尝试是:

tibble_joined %>%
  group_by(AccountNumber) %>%
  mutate(Tibble3 = tibble(tibble2 / tibble1))

tibble_joined %>%
  group_by(AccountNumber) %>%
  summarise(Tibble3 = tibble2 / tibble1)

这两个都给出了这个错误:

Error in mutate_impl(.data, dots) : 
  Evaluation error: non-numeric argument to binary operator.

我试图找到这个问题的优雅解决方案,但我找不到任何东西。

=============================================== ==========================

我完全清楚我的问题可以通过以下方式解决:

tibble_Main %>%
  group_by(AccountNumber) %>%
  unnest() %>%
  mutate(A_Ratio = A_2 / A_1,
         B_Ratio = B_2 / B_1,
         C_Ratio = C_2 / C_2)

...生成以下内容:

# A tibble: 3 x 10
# Groups: AccountNumber [3]
  AccountNumber   A_1   B_1   C_1   A_2   B_2   C_2 A_Ratio B_Ratio C_Ratio
          <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>
1             1  1.00  2.00  3.00  4.00  5.00  6.00    4.00    2.50    1.00
2             2  1.00  2.00  3.00  4.00  5.00  6.00    4.00    2.50    1.00
3             3  1.00  2.00  3.00  4.00  5.00  6.00    4.00    2.50    1.00

...但这看起来很麻烦,并且会因许多专栏而烦恼。

1 个答案:

答案 0 :(得分:1)

我们可以使用{ foo: number; baz: boolean; }中的map2将一个purrr划分为另一个

tibble

注意:library(purrr) res <- tibble_joined %>% mutate(Tibble3 = map2(Tibble1, Tibble2, ~ as_tibble( .y/.x) %>% rename_all(funs(sub('_.*', "_ratio", .))))) res # A tibble: 3 x 4 # AccountNumber Tibble1 Tibble2 Tibble3 # <int> <list> <list> <list> #1 1 <tibble [1 x 3]> <tibble [1 x 3]> <tibble [1 x 3]> #2 2 <tibble [1 x 3]> <tibble [1 x 3]> <tibble [1 x 3]> #3 3 <tibble [1 x 3]> <tibble [1 x 3]> <tibble [1 x 3]> res$Tibble3 #[[1]] # A tibble: 1 x 3 # A_ratio B_ratio C_ratio # <dbl> <dbl> <dbl> #1 4.00 2.50 2.00 #[[2]] # A tibble: 1 x 3 # A_ratio B_ratio C_ratio # <dbl> <dbl> <dbl> #1 4.00 2.50 2.00 #[[3]] # A tibble: 1 x 3 # A_ratio B_ratio C_ratio # <dbl> <dbl> <dbl> #1 4.00 2.50 2.00 purrr个套件

的一部分