'Problem with spreading a data frame using R [closed]

I am very new using R - I have a big data frame that looks in this structure:

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

I want to transform this into a spread mode like:

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

I have tried using the spread() function in R:

file %>% 
  spread(file$sample, file$counts)

I get this error

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.

Am I missing something? Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source