[R] data

Thomas Lumley tlumley at u.washington.edu
Mon Apr 29 20:36:21 CEST 2002


On Mon, 29 Apr 2002, sonchawan tamkaew wrote:

> Hello,
>
> Suppose I have a matrix with 1000 rows and 3 columns, 1st column date, 2nd
> one is type, 3rd one is yield. So it looks like this.
>
> date        type    yield
> 01/01/01    A       3.4
> 01/01/01    B       3.2
> 01/01/01    C       1.8
> 02/01/01    B       3.3
> 03/01/01    A       3.3
> 03/01/01    B       3.4
> 04/01/01    A       3.5
> ...         .       ...
> ...         .       ...
>
> I would like to have the data look like this instead.
>
> date         A     B    C
> 01/01/01    3.4   3.2  1.8
> 02/01/01          3.3
> 03/01/01    3.3   3.4
> 04/01/01    3.5
>
> The missing values could be either blank or NA.
>
> How could I do this without writing for/if loop or do it in Excel before?

Use the reshape() command

> reshape(data,idvar="date",timevar="type",direction="wide")
      date yield.A yield.B yield.C
1 01/01/01     3.4     3.2     1.8
4 02/01/01      NA     3.3      NA
5 03/01/01     3.3     3.4      NA
7 04/01/01     3.5      NA      NA


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list