[R] Frame from long to wide format
Tom Backer Johnsen
backer at psych.uib.no
Tue Jun 10 22:59:01 CEST 2008
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?
More information about the R-help
mailing list