Re: [R] Frame from long to wide format

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Tue, 10 Jun 2008 15:26:00 -0600

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.
>
______________________________________________ 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 Tue 10 Jun 2008 - 21:33:02 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 Tue 10 Jun 2008 - 22:30:53 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