[R] reshape data frame
arun
smartpink111 at yahoo.com
Sat Nov 16 00:56:59 CET 2013
Hi,
Try:
var1 <- load("reshape_data.frame.RData")
##It is better not to name the objects with function names.
dat1 <- data
reshape1 <- reshape
names(dat1)[grep("X\\d+",names(dat1))] <- gsub("[[:alpha:]]","X_",names(dat1)[grep("X\\d+",names(dat1))])
res1 <- reshape(dat1,direction="long",varying=7:ncol(dat1),sep="_")
res2 <- res1[with(res1,order(Yr,Seas,Flt.Svy,Gender,Part,NSAMP)),-9]
row.names(res2) <- 1:nrow(res2)
colnames(res2) <- colnames(reshape1)
all.equal(res2,reshape1)
#[1] TRUE
A.K.
Some advice on transforming my data would be appreciated. Attached is
an .Rdata image with my examples (reshape_data.frame.Rdata). Within the
image are 2 objects:
1) The "data" object contains an example of my original data
format. The columns "X20" : "X50" are histogram bins, and the rows
beneath are the number of observations (ie. counts) within those bins.
The other columns; "Yr", "Seas", "Flt.Svy", "Gender", "Part", "NSAMP"
are observation associated with each bin count.
2) The "reshape" object is the format I need to transform the "data" object into.
I think the reshape() function is designed for this, and my
"data" object is in wide format, and my "resphape" object would be in
long format. If indeed reshape() is the best way to do this, I'm
looking for help in calling the function to transform my data.
Many thanks,
Tim
More information about the R-help
mailing list