如果spread能正常工作,pivot_wider列索引错误

时间:2019-12-10 16:17:15

标签: r tidyr

尝试在更大的范围内使用数据透视表,即使展开时很容易也会出现以下错误:

Error: Positive column indexes in `[` must match number of columns: * `.data` has 1 columns

一个简单的例子

test
# A tibble: 10 x 3
   CaseNumber ID    count_number
        <dbl> <chr>        <int>
 1       668. A                1
 2       659. D                1
 3       661. E                1
 4       661. G                1
 5       639. B                1
 6       676. G                2
 7       728. C                1
 8       676. G                3
 9       717. F                1
10       734. H                1

pivot_wider(test, names_from = count_number, values_from = CaseNumber)

Error: Positive column indexes in `[` must match number of columns:
* `.data` has 1 columns
* Position 2 equals 6
* Position 3 equals 8

是否包含其他参数(如id_cols或values_fill)都没有关系,也会发生相同的错误。我在R 3.6.0和tidyverse 1.3.0中。好像我缺少明显的东西...
相反,传播效果符合我的预期:

spread(test, count_number, CaseNumber)
# A tibble: 8 x 4
  ID      `1`   `2`   `3`
  <chr> <dbl> <dbl> <dbl>
1 A      668.   NA    NA 
2 B      639.   NA    NA 
3 C      728.   NA    NA 
4 D      659.   NA    NA 
5 E      661.   NA    NA 
6 F      717.   NA    NA 
7 G      661.  676.  676.
8 H      734.   NA    NA 

0 个答案:

没有答案