Question

I have a data set from a lab experiment. The data is now in this form:

ColumnID Soiltype Treatment Concentration1 Concentration2 Concentration3
1        Oh       Control   29             31             34
2        LOf      Treatment 100            99             94
3        Oh       Treatment 80             83             78

How can I manipulate the data such that it looks like the following:

ColumnID Soiltype Treatment Concentration
1        Oh       Control   29
1        Oh       Control   31
1        Oh       Control   34
2        LOf      Treatment 100
2        LOf      Treatment 99
2        LOf      Treatment 94
3        Oh       Treatment 80
3        Oh       Treatment 83
3        Oh       Treatment 78

What can I try next?

Était-ce utile?

La solution

This goes under the general category of reshaping data to long form (for your future google-fu reference):

library(reshape2)

melt(df, id.var = c('ColumnID', 'Soiltype', 'Treatment'),
         value.name = "Concentration")
#  ColumnID Soiltype Treatment       variable Concentration
#1        1       Oh   Control Concentration1            29
#2        2      LOf Treatment Concentration1           100
#3        3       Oh Treatment Concentration1            80
#4        1       Oh   Control Concentration2            31
#5        2      LOf Treatment Concentration2            99
#6        3       Oh Treatment Concentration2            83
#7        1       Oh   Control Concentration3            34
#8        2      LOf Treatment Concentration3            94
#9        3       Oh Treatment Concentration3            78

Removal of extra columns and ordering is left to the reader.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top