Home > Blockchain >  Problem with spreading a data frame using R
Problem with spreading a data frame using R

Time:02-28

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

CodePudding user response:

Development on spread() is complete, and for new code we recommend switching to pivot_wider(), which is easier to use, more featureful, and still under active development. df %>% spread(key, value) is equivalent to 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
  • Related