jeudi 27 avril 2017

Populating a table on R

I have the following tab delimited file (read into my code under variable name "data"):

Species Salt    Inhibited
C. violaceum    Cadmium nitrate 1
C. violaceum    Cadmium chloride    1
C. violaceum    Cobalt chloride 1
C. violaceum    Cobalt nitrate  1
C. violaceum    Iron (III) chloride 0
C. violaceum    Iron (III) sulfate  0
C. violaceum    Iron (II) sulfate   0
C. violaceum    Manganese chloride  0
C. violaceum    Manganese sulfate   0
C. violaceum    Nickel chloride 0
P. aeruginosa   Cadmium nitrate 1
P. aeruginosa   Cadmium chloride    1
P. aeruginosa   Cobalt chloride 1
P. aeruginosa   Cobalt nitrate  1
P. aeruginosa   Iron (III) chloride 0
P. aeruginosa   Iron (III) sulfate  0
P. aeruginosa   Iron (II) sulfate   0
P. aeruginosa   Manganese chloride  0
P. aeruginosa   Manganese sulfate   0
P. aeruginosa   Nickel chloride 1
S. marcescens   Cadmium nitrate 1
S. marcescens   Cadmium chloride    1
S. marcescens   Cobalt chloride 1
S. marcescens   Cobalt nitrate  1
S. marcescens   Iron (III) chloride 0
S. marcescens   Iron (III) sulfate  0
S. marcescens   Iron (II) sulfate   0
S. marcescens   Manganese chloride  0
S. marcescens   Manganese sulfate   0
S. marcescens   Nickel chloride 1

I would like it to be put into a table in the format:

Salt    No.Inhibited    Species.Inhibited
Cadmium nitrate    3    C. violaceum, P. aeruginosa, S. marcescens
Iron (III) chloride     0    None
Nickel chloride    2    P. aeruginosa, S. marcescens

Etc. (I would like all the data included but only typed a short amount here) So far I have managed to make a table that shows The Salt in the first column and the No. Inhibited in the second column using the following code:

data <- read.delim("tabledata.txt", header=TRUE, sep="\t")
data1 <- aggregate(Inhibited~Salt, data=data, FUN = sum)

But I can't get the species that were inhibited to show up in a third column. I've tried using a for loop with an ifelse statement:

for(Species1 in data$Inhibited)
 ifelse (data$Inhibited == 1,yes=data$Species, no="None")
data3 <- cbind.data.frame(data1, Species1)

but this only creates a third column with the value "1" in each row. My professor suggested I use dcast (from the reshape2 package) to try to make this work, but I can't figure that out either. Can someone give me some direction on creating this third column?

Aucun commentaire:

Enregistrer un commentaire