假设我有以下格式的嵌套小标题:
# 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
...但是这似乎很麻烦,并且会引起许多专栏的困扰。
最佳答案
我们可以使用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
是tidyverse
软件包的一部分