[R] simple data transformation question
Henrique Dallazuanna
wwwhsd at gmail.com
Tue Mar 2 14:45:23 CET 2010
Try this:
reshape(cbind(id = as.numeric(dtf$var), dtf, time = with(dtf,
ave(value, var, FUN = seq))), timevar="time", direction="wide")
Or:
xtabs(value ~ var + ave(value, var, FUN = seq), data = dtf)
On Tue, Mar 2, 2010 at 9:40 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> Hi all,
> I have a (hopefully) simple newbie-level question.
>
> # I have data like this:
> dtf <- data.frame(read.table(textConnection("var value
> company 9887.1
> company 91117.0
> blaah 91.1
> etc 11
> etc 97111"), header=TRUE))
>
> # I would like to have output like this (the index number may vary):
> var value.1 value.2
> company 9887.1 91117.0
> blah 91.1 NA
> etc 11 97111
>
> # I tried the following.
> library(reshape)
> cast(dtf, var~value, mean) # 'mean' because some function needs to be specified.
> ... this does not what I want, nor does t(dtf).
>
> Can somebody help me with the correct transformation, or at least with which function to use best? Thank you in advance!
>
> Cheers!!
> Albert-Jan
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In the face of ambiguity, refuse the temptation to guess.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
> [[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.
>
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list