Home > Software design >  Removing list format from cell in R
Removing list format from cell in R

Time:09-14

I'm using pivot wider on some data below:

df <- structure(list(Gene.Symbol = c("DES", "AARS2", "ABCC9", "ABCC9", 
"ABCC9"), PanelApp = c("Arrhythmogenic cardiomyopathy", "Cardiomyopathies - including childhood onset", 
"Cardiomyopathies - including childhood onset", "Dilated cardiomyopathy - adult and teen", 
"Dilated Cardiomyopathy and conduction defects")), row.names = c(NA, 
5L), class = "data.frame")

df %>% pivot_wider(names_from=Gene.Symbol, 
    values_from = PanelApp) -> pa_wider

This gives me this output, but I'm ending up with lists printed:

pa_wider <- structure(list(DES = list(c("Arrhythmogenic cardiomyopathy", 
"Cardiomyopathies - including childhood onset"
)), AARS2 = list("Cardiomyopathies - including childhood onset"), 
    ABCC9 = list(c("Cardiomyopathies - including childhood onset", 
    "Dilated cardiomyopathy - adult and teen", "Dilated Cardiomyopathy and conduction defects"
    ))), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"
))

The output is as a list (c("value1", "value2")) and I instead want the cells to look like this (value1, value2):

structure(list(DES = ("Arrhythmogenic cardiomyopathy, Cardiomyopathies - including childhood onset"
), AARS2 = ("Cardiomyopathies - including childhood onset"), 
ABCC9 = ("Cardiomyopathies - including childhood onset, Dilated cardiomyopathy - adult and teen, Dilated Cardiomyopathy and conduction defects"
)), row.names = c(NA, -1L), class = c("data.frame"
))

Can anyone help please?

CodePudding user response:

Perhaps you want to group by the gene and then summarize the PanelApp data using paste0:

df %>% 
  group_by(Gene.Symbol) %>% 
  summarize(PanelApp = paste0(PanelApp, collapse = ", ")) %>% 
  pivot_wider(names_from=Gene.Symbol, 
              values_from = PanelApp)

It looks hard to read though. Is that what you want?

  • Related