-5

I have a Matrix as shown:

A                B          
c("A,"B","A")    c("Pink","blue")

What I want:

A                B
A,B              Pink,Blue

Data from dput():

structure(list(c("PARP", "BRCA1", "BRCA2", "PARP-1/2", "BRCA" ), 
               c("ovarian, fallopian tube", "peritoneal cancer", "tumor", 
                 "toxicity", "ovarian cancer", "thrombocytopenia", "fatigue", 
                 "nausea", "leukopenia", "neutropenia", "vomiting", "anemia"), 
               NULL, 
               c("veliparib", "Veliparib", "platinum"), 
               "patients", 
               25818403), 
          .Dim = c(1L, 6L), 
          .Dimnames = list(NULL, 
                           c("Genes", "Diseases", "Mutations", 
                             "Chemicals", "Species", "PMID"))) 

I tried using seperator function and strsplit function but could not achieve the desired results.Any help would be appreciated. Thanks!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 5
    Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Using `dput()` to share sample data so we can see the class of the objects involved. It's unclear from your text representation what you actually want. – MrFlick Oct 03 '17 at 14:38
  • @AndrewGustar! Thanks for the Edit.How did you do that? – Bioinformatician Oct 03 '17 at 14:38
  • @Bioinformatician - select the code text, then click the `{}` icon on the editor. Or put four spaces in front of each line. – Andrew Gustar Oct 03 '17 at 14:42
  • My bad! I just checked for the class and it came up as "Matrix" – Bioinformatician Oct 03 '17 at 14:47
  • Thank you, but this is still a little unclear, could you please `dput()` as MrFlick suggested so that we can see exactly what it is? – Hack-R Oct 03 '17 at 14:49
  • structure(list(c("PARP", "BRCA1", "BRCA2", "PARP-1/2", "BRCA" ), c("ovarian, fallopian tube", "peritoneal cancer", "tumor", "toxicity", "ovarian cancer", "thrombocytopenia", "fatigue", "nausea", "leukopenia", "neutropenia", "vomiting", "anemia"), NULL, c("veliparib", "Veliparib", "platinum"), "patients", 25818403), .Dim = c(1L, 6L), .Dimnames = list(NULL, c("Genes", "Diseases", "Mutations", "Chemicals", "Species", "PMID"))) – Bioinformatician Oct 03 '17 at 14:57

3 Answers3

0

Perhaps this will work for you

ans <- apply(M, 2, function(i) toString(unlist(i)))

ans[1]
#                                Genes 
# "PARP, BRCA1, BRCA2, PARP-1/2, BRCA"
CPak
  • 13,260
  • 3
  • 30
  • 48
0

Your data is matrix. That is why you are seeing in form of a list. You can access it using matrix index or you can convert it into data frame then access using its column as below:

##Your Data:

M1 <- structure(list(c("PARP", "BRCA1", "BRCA2", "PARP-1/2", "BRCA" ), c("ovarian, fallopian tube", "peritoneal cancer", "tumor", "toxicity", "ovarian cancer", "thrombocytopenia", "fatigue", "nausea", "leukopenia", "neutropenia", "vomiting", "anemia"), NULL, c("veliparib", "Veliparib", "platinum"), "patients", 25818403), .Dim = c(1L, 6L), .Dimnames = list(NULL, c("Genes", "Diseases", "Mutations", "Chemicals", "Species", "PMID"))) 

class(M1)
#[1] "matrix"

### It's matrix. So, to access it, you use row number and column number

M1[1, 1]
#$Genes
#[1] "PARP"     "BRCA1"    "BRCA2"    "PARP-1/2" "BRCA"  

M1[1, 2]
#$Diseases
#[1] "ovarian, fallopian tube" "peritoneal cancer"       "tumor"                  
#[4] "toxicity"                "ovarian cancer"          "thrombocytopenia"       
#[7] "fatigue"                 "nausea"                  "leukopenia"             
#[10] "neutropenia"             "vomiting"                "anemia"

## You can also convert to data frame then access it by column as below:

# Converting your matrix M1 to data.frame
df <- as.data.frame(M1)

class(df)
#[1] "data.frame"

df$Genes
$Genes
#[1] "PARP"     "BRCA1"    "BRCA2"    "PARP-1/2" "BRCA"

##You can do unlist your list
unlist(df$Genes)
#Genes1     Genes2     Genes3     Genes4     Genes5 
#"PARP"    "BRCA1"    "BRCA2" "PARP-1/2"     "BRCA"

## You can convert it into data frame after you unlist 
data.frame(unlist(df$Genes))
#           unlist.df.Genes.
# Genes1             PARP
# Genes2            BRCA1
# Genes3            BRCA2
# Genes4         PARP-1/2
# Genes5             BRCA

I hope this helps you.

Santosh M.
  • 2,356
  • 1
  • 17
  • 29
-1

using do.call,bind_rows did the job!Thank you for your answers!