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

From: hadley wickham <h.wickham_at_gmail.com>
Date: Tue, 17 Jun 2008 15:15:35 -0500

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/

______________________________________________
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 17 Jun 2008 - 20:58:27 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 17 Jun 2008 - 22:30:42 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