[R] transpose? reshape? flipping? challenge with data frame

Patrick Hausmann patrick.hausmann at uni-bremen.de
Sat Apr 24 07:56:40 CEST 2010


Ups, I mean library(reshape) not plyr, sorry

# 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(reshape)
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

HTH,
Patrick

Am 23.04.2010 12:16, schrieb Patrick Hausmann:
> 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



More information about the R-help mailing list