Re: [R] Melt (reshape) question

From: jim holtman <jholtman_at_gmail.com>
Date: Tue, 15 Jul 2008 18:03:30 -0400

Probably as the result of read.table without any headers, the column names are "V1"..."V720". You need to add new column names. You have statements:

colnames <- columnnames
rownames <- rnames

Are you assuming that these are assigning row/column names to the structure? Try

colnames(PopDen) <- columnnames

On Tue, Jul 15, 2008 at 5:00 PM, Steve Murray <smurray444_at_hotmail.com> wrote:
>
> Dear all,
>
> I
> have a grid of 720 columns by 360 rows of global population density
> values, and hope to convert this to column format using the 'melt' command in the 'reshape' package. I'm not receiving
> any errors as such, but when the code has finished running, my output
> looks like this:
>
>> head(PopDens.long)
> Latitude Longitude PopDensity
> 1 -84.75 V1 0
> 2 -84.25 V1 0
> 3 -83.75 V1 0
> 4 -83.25 V1 0
> 5 -82.75 V1 0
> 6 -82.25 V1 0
>
>> tail(PopDens.long)
> Latitude Longitude PopDensity
> 232658 87.25 V720 -99
> 232659 87.75 V720 -99
> 232660 88.25 V720 -99
> 232661 88.75 V720 -99
> 232662 89.25 V720 -99
> 232663 89.75 V720 -99
>
> As
> you can see, I'm getting strange 'V' values in the 'Longitude' column,
> which don't correspond to my real Longitude values. I've had a good
> look through the script and tried tweaking a few things, but with no
> luck. I've pasted the code below, so that you can see what I've done,
> and maybe you someone spot a fundamental error of mine, or something that
> I'm missing.
>
>
> columnnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length = 720))
> rnames <- sprintf("%.2f", seq(from = 89.75, to = -89.75, length = 360))
>
> read.table("gpw_pop_density_hd_1995a.asc", colClasses = "numeric", na = "-99") -> PopDens
>
> colnames <- columnnames
> rownames <- rnames
>
> library(reshape)
>
> PopDens$Latitude <- rownames
>
> PopDens.long <- melt(PopDens, id.var = "Latitude", na.rm = TRUE)
> names(PopDens.long) <- c("Latitude", "Longitude", "PopDensity")
>
> head(PopDens.long)
>
>
> Am
> I right in thinking that this *should* also remove the -99s (NA
> values)? Because at present, the lower end ot the file looks like this:

>
>> tail(PopDens.long)
> Latitude Longitude PopDensity
> 232658 -87.25 V720 -99
> 232659 -87.75 V720 -99
> 232660 -88.25 V720 -99
> 232661 -88.75 V720 -99
> 232662 -89.25 V720 -99
> 232663 -89.75 V720 -99
>
> Again,
> I've almost certainly made an error somewhere! In any case, is there
> also a way of telling the code to also ignore -88 values as well as the -99s?
>
> Many thanks for your help.
>
> Best wishes,
>
> Steve
>
> _________________________________________________________________
> The John Lewis Clearance - save up to 50% with FREE delivery
>
> [[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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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 15 Jul 2008 - 22:06:41 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 15 Jul 2008 - 22:31:44 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