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

From: Steve Murray <smurray444_at_hotmail.com>
Date: Tue, 17 Jun 2008 21:05:13 +0000

 > > 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)
 

Sure, this is a dput of the original data frame (the first few rows displayed in the console). Don't be put off by the number of -99.9s, this is normal:

structure(list("-179.75" = c(-99.9, -99.9, -99.9, -99.9, -99.9, -99.9), "-179.25" = c(-99.9, -99.9, -99.9, -99.9, -99.9, -99.9 ), "-178.75" = c(-99.9, -99.9, -99.9, -99.9, -99.9, -99.9), "-178.25" = c(-99.9,

-99.9, -99.9, -99.9, -99.9, -99.9), "-177.75" = c(-99.9, -99.9, 
-99.9, -99.9, -99.9, -99.9), "-177.25" = c(-99.9, -99.9, -99.9, 
-99.9, -99.9, -99.9), "-176.75" = c(-99.9, -99.9, -99.9, -99.9, 
-99.9, -99.9), "-176.25" = c(-99.9, -99.9, -99.9, -99.9, -99.9, 
-99.9), "-175.75" = c(-99.9, -99.9, -99.9, -99.9, -99.9, -99.9
), "-175.25" = c(-99.9, -99.9, -99.9, -99.9, -99.9, -99.9), "-174.75" = c(-99.9,
-99.9, -99.9, -99.9, -99.9, -99.9), "-174.25" = c(-99.9, -99.9, 
-99.9, -99.9, -99.9, -99.9), "-173.75" = c(-99.9, -99.9, -99.9, 
-99.9, -99.9, -99.9), "-173.25" = c(-99.9, -99.9, -99.9, -99.9, 
-99.9, -99.9), "-172.75" = c(-99.9, -99.9, -99.9, -99.9, -99.9, 
-99.9),

And this is a str output of the original data frame (first few rows of that displayed in the console):

`data.frame': 6 obs. of 720 variables:

 $ -179.75: num  -99.9 -99.9 -99.9 -99.9 -99.9 -99.9
 $ -179.25: num  -99.9 -99.9 -99.9 -99.9 -99.9 -99.9
 $ -178.75: num  -99.9 -99.9 -99.9 -99.9 -99.9 -99.9
 $ -178.25: num  -99.9 -99.9 -99.9 -99.9 -99.9 -99.9
 $ -177.75: num  -99.9 -99.9 -99.9 -99.9 -99.9 -99.9


This is a dput of the melted dataframe:

structure(list(variable = c("-179.75", "-179.75", "-179.75", "-179.75", "-179.75", "-179.75"), value = c(-99.9, -99.9, -99.9, -99.9, -99.9, -99.9)), .Names = c("variable", "value"), row.names = c("-89.75", "-89.25", "-88.75", "-88.25", "-87.75", "-87.25"), class = "data.frame")

And finally a str output of the melted data:

`data.frame': 6 obs. of 2 variables:
 $ variable: chr "-179.75" "-179.75" "-179.75" "-179.75" ...  $ value : num -99.9 -99.9 -99.9 -99.9 -99.9 -99.9

These don't mean a huge amount to me, but if they help anyone to diagnose the problem, then that'd be great.

Many thanks again,

Steve



Great deals on almost anything at eBay.co.uk. Search, bid, find and win on

        [[alternative HTML version deleted]]



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 - 21:46:23 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 Wed 18 Jun 2008 - 00:31:04 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