[R] Reshape or Stack? (To produce output as columns)

hadley wickham h.wickham at gmail.com
Tue Jun 17 22:15:35 CEST 2008


On Tue, Jun 17, 2008 at 1:47 PM, Steve Murray <smurray444 at hotmail.com> wrote:
>
> Dear all,
>
> Many thanks for the suggestions put forward. I've decided to go with the 'melt' command from the 'reshape' library, as this seems to run the quickest.
>
> I do have a couple of questions however, regarding the use of the 'melt' command. Below are the last few lines of the 'melted' data. The first column shows the latitude values (the row names of the former data frame), the second column are the longitude values (the column names of the former data frame) and the third column shows the actual values (-99.9 is correct in this case).
>
> 85.25719    179.75 -99.9
> 85.75719    179.75 -99.9
> 86.25719    179.75 -99.9
> 86.75719    179.75 -99.9
> 87.25719    179.75 -99.9
> 87.75719    179.75 -99.9
> 88.25719    179.75 -99.9
> 88.75719    179.75 -99.9
> 89.25719    179.75 -99.9
> 89.75719    179.75 -99.9
>
>
> As you can see, each value in the 'latitude' column is followed by '719'. As far as I can tell, this value seems to represent the number of times the value is repeated in this column (I could be wrong though). Remember that this is the end of a fairly sizeable dataset - these 'surplus' figures range from '1' further up the column to '719' as shown here. How do I go about removing these values?
>
> Also, I hope to add headings to these columns ("Latitude", "Longitude", "Value"). The best I've been able to manage so far is by using the 'names' command, however this only allows me to add headings to the second and third columns. Below is what happens when I've tried to add headings to all three:
>
>> names(melted) <- c("Latitude", "Longitude", "Value")
> Error in "names<-.default"(`*tmp*`, value = c("Latitude", "Longitude",  :
>        'names' attribute [3] must be the same length as the vector [2]
>
> If anyone is able to offer any advice and suggestions as to how I might overcome these issues, then I'd be very grateful.

It sounds like something is going wrong with the melting.  Could you
please include the output of str(original data frame), and
str(melted)?  (Or even better a small version of your data created
with dput)

Hadley

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



More information about the R-help mailing list