0

the df I'm working on originally is a long format table like below:-

VALUE Gene_Symbol Sample_ID
12253 BRCA P1
42356 CAMP P2

Then for generating the DGEList, I decided to transform it into a wide format and generated below table:

P1 P2 P3 P4
null 2423 46456 74564 523424
CAMP 42356
BRCA 12253 453 658665

because some samples may not express a certain gene, hence the console leave it blank when I wide pivot it. When I view() the df, it showed as blank. But when I do summery() it shows as NULL in the console.

Right now, I am trying to use apply() to replace the blank with 0 but with no luck, all values turned into 0.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
Spybuster
  • 19
  • 5

1 Answers1

1

The values_fill function in tidy::pivot_wider should do the trick:

tidyr::pivot_wider(df, 
                     names_from = Sample_ID, 
                     values_from = VALUE, 
                     values_fill = 0)

Output:

#   Gene_Symbol    P1    P2
#   <chr>       <int> <int>
# 1 BRCA        12253     0
# 2 CAMP            0 42356

Data

df <- read.table(text = "VALUE  Gene_Symbol Sample_ID
12253   BRCA    P1
42356   CAMP    P2
", h = T)

Generally, you could replace the values of NA in a data frame without apply using something like this:

df_na <- tidyr::pivot_wider(df, names_from = Sample_ID, values_from = VALUE)

#   Gene_Symbol    P1    P2
#   <chr>       <int> <int>
# 1 BRCA        12253    NA
# 2 CAMP           NA 42356

df_na[is.na(df_na)] <- 0

#   Gene_Symbol    P1    P2
#   <chr>       <int> <int>
# 1 BRCA        12253     0
# 2 CAMP            0 42356
jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • @Spybuster Try the `df_na[is.na(df_na)] <- 0` approach in the edit. If you provide sample data using `dput()`, it would help provide more specific assistance – jpsmith Feb 27 '23 at 12:16
  • Try `values_fill = list(value = 0)`? You can certainly share data example if you make some up. Example data does not have to be real data, just something we can recreate your problem with. I made some by translating your table, and the code worked. So there if that doesn’t work on your read data we will need more information to help – jpsmith Feb 27 '23 at 12:30
  • See the following link to see some ideas: [https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – jpsmith Feb 27 '23 at 12:57
  • 1
    Yes, its probably an issue with your data (ie, an errand blank space making R interpret it as a character or something) – jpsmith Feb 27 '23 at 19:12