Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with spreading a data frame using R

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading