Re: [R] Frame from long to wide format

From: hadley wickham <h.wickham_at_gmail.com>
Date: Tue, 10 Jun 2008 16:37:35 -0500

The reshape package will definitely do what you want. Cast will automatically fill in an missing spots with NAs, and you'll just need something like the following for your aggregation function:

last <- function(x) x[length(x)]

You can find out more about reshape at http://had.co.nz/reshape

Hadley

On Tue, Jun 10, 2008 at 4:26 PM, Greg Snow <Greg.Snow_at_imail.org> wrote:
> 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.
>

-- 
http://had.co.nz/

______________________________________________
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 - 22:01:26 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 - 23:30:48 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