[R] Frame from long to wide format

Greg Snow Greg.Snow at imail.org
Tue Jun 10 23:26:00 CEST 2008


The reshape function and even more powerful reshape package will probably do what you want.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111



> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Tom Backer Johnsen
> Sent: Tuesday, June 10, 2008 2:59 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Frame from long to wide format
>
> I have this frame in the long format which I want to convert
> to wide format.  There are three columns in the frame, the id
> for the rows in the wide format, the name of the column where
> the final value (the third
> column) is to be placed.
>
> The complicating factor is that the long format is not
> complete in at least two ways, (a) there may be more than one
> reference to one cell in the wide format, in that case I want
> to use the last one, and (b) there may be no references to a
> cell in the wide format, and in that case I want the cell to be an NA.
>
> I have looked at and done some experiments with the unstack
> () function, this does not seem to do what I want.  For
> instance, if there are more than one reference to a cell in
> the wide format, an extra row may be added or a list
> structure is produced.
>
> What I want is to generate a frame in a "loose" manner, more
> or less by the generation of a frame with the number of rows
> corresponding to the individuals (unique values) and columns
> (also unique values).  If there are no references in the long
> format to a given cell, then the value should be NA, if there
> are more than one, the last one should be used.
>
> I can see (I think) how I could program this by the
> generation of a frame with the correct row and column names,
> fill that with NA's, and then use a for loop to assign a
> value for the long format to the correct cell in the frame.
> That is not elegant, but would work.  Is it possible to be
> pointed towards alternatives?
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list