[R] transpose? reshape? flipping? challenge with data frame
Patrick Hausmann
patrick.hausmann at uni-bremen.de
Fri Apr 23 12:16:20 CEST 2010
Hi David,
you could use a mix of "plyr" and reshape:
# Example datasets
# Input
propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"),
R209120812=c(NA, 0.49, 0.38, 0.04, 0.09),
R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10))
library(plyr)
xpropsum <- melt(propsum, id.var="coverClass", variable_name = "Image")
tpropsum <- reshape(xpropsum, timevar="coverClass", idvar="Image",
direction="wide")
colnames(tpropsum) <- sub("value.", "", colnames(tpropsum))
tpropsum
Cheers
Patrick
Am 23.04.2010 06:43, schrieb David.Gobbett at csiro.au:
> Greetings all,
>
> I am having difficulty transposing, reshaping, flipping (not sure which) a data frame which is read from a DBF file. I have tried using t(), reshape() and other approaches without success. Can anyone please suggest an way (elegant or not) of flipping this data around ?
>
> The initial data is like propsum (defined below), and I want it to look like tpropsum once reformed.
>> propsum
> coverClass R209120812 R209122212
> 1 C NA 0.05
> 2 G 0.49 0.35
> 3 L 0.38 0.41
> 4 O 0.04 0.09
> 5 S 0.09 0.10
>
>> tpropsum
> Image C G L O L.1
> 1 R209120812 NA 0.49 0.38 0.04 0.09
> 2 R209122212 0.05 0.35 0.41 0.09 0.10
>
> # Example datasets
> # Input
> propsum<- data.frame(coverClass=c("C", "G", "L", "O", "S"),
> R209120812=c(NA, 0.49, 0.38, 0.04, 0.09),
> R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10))
>
> # Desired output
> tpropsum<- data.frame(Image=c("R209120812", "R209122212"),
> C=c(NA, 0.05),
> G=c(0.49, 0.35),
> L=c(0.38, 0.41),
> O=c(0.04, 0.09),
> L=c(0.09, 0.10))
>
> Thanks,
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list