0

我希望计算 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但无法自己制定解决方案。

有人可以启发我吗?

4

2 回答 2

1

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

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 
于 2022-02-25T08:59:57.573 回答
0

可能不需要如此复杂的数据处理,例如你可以做矩阵减法:

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
于 2022-02-25T08:59:35.073 回答