我希望计算 tibble 中的每组数据与基线数据集有何不同。
为了计划,我编写了这个 R 代码来从另一个相同大小的中减去一个 tibble:
# this works
tbl_a <- tibble(a1 = 1, a2 = 2, a3 = 3)
tbl_b <- tibble(a1 = 4, a2 = 5, a3 = 6)
tbl_a + tbl_b
# > tbl_a + tbl_b
# a1 a2 a3
# 1 5 7 9
现在我把它变成一组 tibbles
# compare multiple datasets to baseline of same shape
tbl_a1 <- tibble(id = "i", a1 = 1, a2 = 2, a3 = 3)
tbl_a2 <- tibble(id = "ii", a1 = 2, a2 = 3, a3 = 4)
tbl_a3 <- tibble(id = "iii", a1 = 3, a2 = 4, a3 = 5)
tbl_base <- tibble(id_base = "baseline", a1 = 4, a2 = 5, a3 = 6)
tbls <- bind_rows(tbl_a1, tbl_a2, tbl_a3)
tbls_compare <- tbls %>%
nest(set = starts_with("a")) %>%
bind_cols(tbl_base) %>%
nest(set_baseline = starts_with("a"))
# id set id_base set_baseline
# <chr> <list> <chr> <list>
# 1 i <tibble [1 × 3]> baseline <tibble [1 × 3]>
# 2 ii <tibble [1 × 3]> baseline <tibble [1 × 3]>
# 3 iii <tibble [1 × 3]> baseline <tibble [1 × 3]>
我希望能够像tbl_a + tbl_b
示例中那样执行减法。
但是,我遇到了一个错误:
> tbls_compare %>%
+ mutate(diff_to_base = set_baseline - set)
Error in `mutate()`:
! Problem while computing `diff_to_base = set_baseline - set`.
Caused by error in `set_baseline - set`:
! non-numeric argument to binary operator
Run `rlang::last_error()` to see where the error occurred.
我尝试使用purrr:map
但无法自己制定解决方案。
有人可以启发我吗?