Dear R Users,
I am using the reshape package to reformat gridded data into column format using
the code shown below. However, when I display the resulting object, a single
column is fomed (instead of three) and all the latitude values (which should be
in either column one or two) are collected at the bottom. Also, the NA values
aren't removed, despite this being requested in the code.
Code:
# NetCDF file has been read in and is being processed...
arunoff_1986_temp <- get.var.ncdf(netcdf_1036_temp, "arunoff")
# Assign row and column names
columnnames <- sprintf("%.2f", seq(from = -89.75, to = 89.75,
length = 360))
rnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length =
720))
colnames(arunoff_1986_temp) <- columnnames
rownames(arunoff_1986_temp) <- rnames
# Melt into columnar format
arunoff_1986$Longitude <- rownames(arunoff_1986)
# Note: If I do: arunoff_1986$Latitude <- rownames(arunoff_1986) (i.e.
change it to 'Latitude', I get the following: Warning message: In
arunoff_1986$Latitude <- rownames(arunoff_1986) : Coercing LHS to a list ),
thus proceed using 'Longitude' where no warning is apparent.
arunoff_long_1986 <- melt(arunoff_1986, id.var="Longitude",
na.rm=TRUE)
> dim(arunoff_long_1986)
[1] 259560 2
>head(arunoff_long_1986, n=10) # This displays what looks like one single
column, but is in fact two: column entitled "L1" is empty until the
end of the file, as shown by the 'tail' command below.
value L1
1
2
3
4
5
6
7
8
9
10
> tail(arunoff_long_1986, n=10)
value L1
Latitude.351 85.25 Latitude
Latitude.352 85.75 Latitude
Latitude.353 86.25 Latitude
Latitude.354 86.75 Latitude
Latitude.355 87.25 Latitude
Latitude.356 87.75 Latitude
Latitude.357 88.25 Latitude
Latitude.358 88.75 Latitude
Latitude.359 89.25 Latitude
Latitude.360 89.75 Latitude
I'd be very grateful indeed if anyone is able to offer assistance by way of
pointing out what I've done wrong. I've spent a long time working on
this and trying various options, but am srill none the wiser! I'm aiming for
three columns: Latitude, Longitude, 'value'.
Many thanks for any help offered,
Steve
can you provide a reproducible example? On Wed, Apr 8, 2009 at 11:59 AM, Steve Murray <smurray444 at hotmail.com> wrote:> > Dear R Users, > > I am using the reshape package to reformat gridded data into column format using the code shown below. However, when I display the resulting object, a single column is fomed (instead of three) and all the latitude values (which should be in either column one or two) are collected at the bottom. Also, the NA values aren't removed, despite this being requested in the code. > > Code: > > > # NetCDF file has been read in and is being processed... > > arunoff_1986_temp <- get.var.ncdf(netcdf_1036_temp, "arunoff") > > > # Assign row and column names > > columnnames <- sprintf("%.2f", seq(from = -89.75, to = 89.75, length = 360)) > rnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length = 720)) > > colnames(arunoff_1986_temp) <- columnnames > rownames(arunoff_1986_temp) <- rnames > > > # Melt into columnar format > > arunoff_1986$Longitude <- rownames(arunoff_1986) > ? ?# Note: If I do: arunoff_1986$Latitude <- rownames(arunoff_1986) ?(i.e. change it to 'Latitude', I get the following: Warning message: In arunoff_1986$Latitude <- rownames(arunoff_1986) : Coercing LHS to a list ), thus proceed using 'Longitude' where no warning is apparent. > > > arunoff_long_1986 <- melt(arunoff_1986, id.var="Longitude", na.rm=TRUE) > > >> dim(arunoff_long_1986) > [1] 259560 ? ? ?2 > >>head(arunoff_long_1986, n=10) ?# This displays what looks like one single column, but is in fact two: column entitled "L1" is empty until the end of the file, as shown by the 'tail' command below. > ? value L1 > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 > >> tail(arunoff_long_1986, n=10) > ? ? ? ? ? ? value ? ? ? L1 > Latitude.351 85.25 Latitude > Latitude.352 85.75 Latitude > Latitude.353 86.25 Latitude > Latitude.354 86.75 Latitude > Latitude.355 87.25 Latitude > Latitude.356 87.75 Latitude > Latitude.357 88.25 Latitude > Latitude.358 88.75 Latitude > Latitude.359 89.25 Latitude > Latitude.360 89.75 Latitude > > > > I'd be very grateful indeed if anyone is able to offer assistance by way of pointing out what I've done wrong. I've spent a long time working on this and trying various options, but am srill none the wiser! I'm aiming for three columns: Latitude, Longitude, 'value'. > > Many thanks for any help offered, > > Steve > > ______________________________________________ > 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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Genius! Thanks very much Hadley - that was surprisingly easier to solve than I was anticipating! As a way of offering something back in return, I don't know if you plan to release a new version of the reshape package, but here's a suggestion to consider, just in case you do. On the basis of what I've learned from this experience, how about including a simple line of code in the next release, along the lines of is.data.frame(object_name) - if TRUE then proceed as normal, if FALSE then issue a warning/error message to inform the user that the procedure may not execute as intended and suggest coercing to a data frame. Apologies if I've overlooked something fundamental which prevents the need or feasibility of this idea - just thought I'd bring it up in case it is useful! Thanks again and all the best, Steve