R - 计算相同大小的嵌套 tibbles

R - Calculation on nested tibbles of the same size

我想计算 tibble 中的每组数据与基线数据集有何不同。

为了计划,我编写了这段 R 代码以从另一个相同大小的小标题中减去一个小标题:

# 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

现在我要把它变成一组小点心

# 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 但我自己无法找到解决方案。

有人能赐教吗?

可能不需要这么复杂的数据处理,比如做矩阵减法即可:

a_matrix <- matrix(c(1, 2, 3, 2, 3, 4, 3, 4, 5), nrow = 3, byrow = TRUE)
a <- c(4, 5, 6)

t(a - a_matrix)

     [,1] [,2] [,3]
[1,]    3    3    3
[2,]    2    2    2
[3,]    1    1    1

我认为您需要添加 rowwise,否则 set_baseline - set 将尝试从 tibbles 列表中减去 tibbles 列表。

tbls_compare <- tbls %>%
  nest(set = starts_with("a")) %>%
  bind_cols(tbl_base) |>
  nest(set_baseline = starts_with("a")) |>
  rowwise() |>
  mutate(diff_to_base = list(as_tibble(set_baseline - set)))

tbls_compare

# A tibble: 3 × 5
# Rowwise: 
  id    set              id_base  set_baseline     diff_to_base    
  <chr> <list>           <chr>    <list>           <list>          
1 i     <tibble [1 × 3]> baseline <tibble [1 × 3]> <tibble [1 × 3]>
2 ii    <tibble [1 × 3]> baseline <tibble [1 × 3]> <tibble [1 × 3]>
3 iii   <tibble [1 × 3]> baseline <tibble [1 × 3]> <tibble [1 × 3]>

这是未嵌套时的样子:

tbls_compare |>
  unnest(cols = diff_to_base)

+ # A tibble: 3 × 7
  id    set              id_base  set_baseline        a1    a2    a3
  <chr> <list>           <chr>    <list>           <dbl> <dbl> <dbl>
1 i     <tibble [1 × 3]> baseline <tibble [1 × 3]>     3     3     3
2 ii    <tibble [1 × 3]> baseline <tibble [1 × 3]>     2     2     2
3 iii   <tibble [1 × 3]> baseline <tibble [1 × 3]>     1     1     1