[R] rearrange data

Stefan Uhmann stefan.uhmann at mailbox.tu-dresden.de
Wed Jun 25 19:42:40 CEST 2008


Dear HelpeRs,

i have a data.frame df as follows:

 >
df <- data.frame(id=rep(1:3,rep(10,3)),
		emoqu=as.factor(rep(c(0,0,1,1,2,2,3,3,4,4),3)),
		x=rnorm(30), y=runif(30))


Now, I would like to rearrange the data and it works - regarding the 
variables/columns I would like to obtain:

 >
df2<-reshape(df, timevar="emoqu", idvar="id", direction="wide")


My problem is, that

 > df
    id emoqu           x          y
1   1     0  0.84078379 0.13701977
2   1     0  1.12215014 0.49143070
..

is reshaped to

 > df2
    id        x.0       y.0 ..
1   1  0.8407838 0.1370198 ..
..

(1) How can I influence which of the values is chosen? In my example, I 
would like to obtain the min/max instead of just the first value which 
appears in df and meets the criteria (id=1, emoqu=0).

(2) Can you imagine a routine which decides which function to use 
dependent on another variable (e.g. id: odd - min; even - max).

Thanks,
Stefan



More information about the R-help mailing list