[R] rearrange data

From: Stefan Uhmann <stefan.uhmann_at_mailbox.tu-dresden.de>
Date: Wed, 25 Jun 2008 19:42:40 +0200


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



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. Received on Wed 25 Jun 2008 - 17:46:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 25 Jun 2008 - 18:30:46 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive