使用 R 传播数据帧的问题

Problem with spreading a data frame using R

我是 R 的新手 - 我有一个大数据框,结构如下:

miRNAs sample counts
miR-15 DM3 302894
miR-15 DM2 110966
miR-15 DM1 81508
miR-43 NI1 23514 
miR-43 NI2 5324
miR-43 NI3 56324

我想将其转换为如下传播模式:

miRNAs DM3 DM2 DM1 NI1 NI2 NI3
miR-15 302894 110966 81508 0 0 0
miR-43 0 0 0 23514 5324 56324

我试过在 R:

中使用 spread() 函数
file %>% 
  spread(file$sample, file$counts)

我收到这个错误

Error:
! Must extract column with a single valid subscript.
x Subscript `var` has size 72 but must be size 1.
Run `rlang::last_error()` to see where the error occurred.

我错过了什么吗?谢谢

spread() 的开发已经完成,对于新代码 ,我们建议切换到 pivot_wider(),它更易于使用,功能更强大,而且仍然正在积极开发中。 df %>% spread(key, value) 等同于 df %>% pivot_wider(names_from = key, values_from = value)

https://tidyr.tidyverse.org/reference/spread.html

library(dplyr)
library(tidyr)

library(tidyr)
file %>% 
  pivot_wider(
    names_from = sample,
    values_from = counts
    )
# A tibble: 2 × 7
  miRNAs    DM3    DM2   DM1   NI1   NI2   NI3
  <chr>   <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
1 miR-15 302894 110966 81508    NA    NA    NA
2 miR-43     NA     NA    NA 23514  5324 56324