r-更新"tibble"版本时"pivot_log()"参数出错



上下文

我们希望管理从宽格式dat_1到长格式dat_2的数据。为了执行它,我们考虑使用参数为names_pattern = '(.+)_(.+)'tidyr::pivot_longer()。它允许我们收集如框中所示的数据,从input formatoutput format

输入数据

> dat_1
original_id timepoint msp3_mfi msp3_dil pf_aarp_mfi pf_aarp_dil
<chr>       <chr>        <dbl>    <int>       <dbl>       <int>
1 id_005      C_0           10.5      400       22.2          400
2 id_005      D10            8.5      400       10.25         400
3 id_005      D13           11        400       10.2          400
4 id_005      D28            8        400        9.75         400
5 id_005      D60            7        400        0.30         400

使用的R代码

dat_2 <- dat_1 %>%
pivot_longer(
cols = msp3_mfi:pf_aarp_dil,
names_to = c('antigen', 'antigen_dil'),
names_pattern = '(.+)_(.+)',
values_to = c('mfi', 'dil'))

所需输出数据

> dat_2 
original_id timepoint antigen antigen_dil   mfi   dil
<chr>       <chr>     <chr>   <chr>       <dbl> <int>
1 id_005      C_0       msp3    mfi          10.5     NA
2 id_005      C_0       msp3    dil         NA      400
3 id_005      C_0       pf_aarp mfi         22.2     NA
4 id_005      C_0       pf_aarp dil         NA      400
5 id_005      D10       msp3    mfi          8.5     NA
6 id_005      D10       msp3    dil         NA      400
7 id_005      D10       pf_aarp mfi         10.25    NA
8 id_005      D10       pf_aarp dil         NA      400
9 id_005      D13       msp3    mfi         11       NA
10 id_005      D13       msp3    dil         NA      400
11 id_005      D13       pf_aarp mfi         10.2     NA
12 id_005      D13       pf_aarp dil         NA      400
13 id_005      D28       msp3    mfi          8       NA
14 id_005      D28       msp3    dil         NA      400
15 id_005      D28       pf_aarp mfi          9.75    NA
16 id_005      D28       pf_aarp dil         NA      400
17 id_005      D60       msp3    mfi          7       NA
18 id_005      D60       msp3    dil         NA      400
19 id_005      D60       pf_aarp mfi          0.30    NA
20 id_005      D60       pf_aarp dil         NA      400

错误

然而,当我们将de R(版本R 3.6.3(会话从tibble 2.1.3更新到tibble. 3.0.1时,我们得到以下错误。


Error: Assigned data `values_to` must be compatible with existing data.
x Existing data has 4 rows.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.

问题

知道为什么我们在更新tibble包版本时在name_pattern中出现此错误吗?

提前感谢

虽然我不知道为什么发生更改,但我想,从本质上讲,这个错误与您的dat_2tibble不整洁有关。将"mfi"one_answers"dil"之间的差异同时包含为自己的列antigen_dil和两个单独的列mfidil是重复的。

根据数据的含义,pivot_longer可以轻松使用的两种格式是:

dat_1 %>%
pivot_longer(
cols = msp3_mfi:pf_aarp_dil,
names_to = c('antigen', '.value'),
names_pattern = '(.+)_(.+)'
)
#> # A tibble: 4 x 5
#>   original_id timepoint antigen   mfi   dil
#>   <chr>       <chr>     <chr>   <dbl> <dbl>
#> 1 id_005      C_0       msp3     10.5   400
#> 2 id_005      C_0       pf_aarp  22.2   400
#> 3 id_005      D10       msp3      8.5   400
#> 4 id_005      D10       pf_aarp  10.2   400

dat_1 %>%
pivot_longer(
cols = msp3_mfi:pf_aarp_dil,
names_to = c('antigen', 'antigen_dil'),
names_pattern = '(.+)_(.+)'
)
#> # A tibble: 8 x 5
#>   original_id timepoint antigen antigen_dil value
#>   <chr>       <chr>     <chr>   <chr>       <dbl>
#> 1 id_005      C_0       msp3    mfi          10.5
#> 2 id_005      C_0       msp3    dil         400  
#> 3 id_005      C_0       pf_aarp mfi          22.2
#> 4 id_005      C_0       pf_aarp dil         400  
#> 5 id_005      D10       msp3    mfi           8.5
#> 6 id_005      D10       msp3    dil         400  
#> 7 id_005      D10       pf_aarp mfi          10.2
#> 8 id_005      D10       pf_aarp dil         400

如果你真的需要你描述的格式的tibble,你可以使用:

dat_1 %>%
pivot_longer(
cols = msp3_mfi:pf_aarp_dil,
names_to = c('antigen', 'antigen_dil'),
names_pattern = '(.+)_(.+)'
) %>% 
mutate(
mfi = if_else(antigen_dil == "mfi", value, NA_real_),
dil = if_else(antigen_dil == "dil", value, NA_real_)
) %>%
select(-value)
#> # A tibble: 8 x 6
#>   original_id timepoint antigen antigen_dil   mfi   dil
#>   <chr>       <chr>     <chr>   <chr>       <dbl> <dbl>
#> 1 id_005      C_0       msp3    mfi          10.5    NA
#> 2 id_005      C_0       msp3    dil          NA     400
#> 3 id_005      C_0       pf_aarp mfi          22.2    NA
#> 4 id_005      C_0       pf_aarp dil          NA     400
#> 5 id_005      D10       msp3    mfi           8.5    NA
#> 6 id_005      D10       msp3    dil          NA     400
#> 7 id_005      D10       pf_aarp mfi          10.2    NA
#> 8 id_005      D10       pf_aarp dil          NA     400

以上代码段使用以下内容创建dat_1:

library(tidyverse)
dat_1 <- 
tribble(
~original_id, ~timepoint, ~msp3_mfi, ~msp3_dil, ~pf_aarp_mfi, ~pf_aarp_dil,
"id_005",      "C_0",           10.5,      400,       22.2,          400,
"id_005",      "D10",            8.5,      400,       10.25,         400
)
dat_1
#> # A tibble: 2 x 6
#>   original_id timepoint msp3_mfi msp3_dil pf_aarp_mfi pf_aarp_dil
#>   <chr>       <chr>        <dbl>    <dbl>       <dbl>       <dbl>
#> 1 id_005      C_0           10.5      400        22.2         400
#> 2 id_005      D10            8.5      400        10.2         400

相关内容

  • 没有找到相关文章

最新更新