Hi all, I have a data frame that looks like such: LATITUDE LONGITUDE TEMPERATURE TIME 36.73 -176.43 58.32 1 50.95 90.00 74.39 1 -30.42 5.45 23.26 1 15.81 -109.31 52.44 1 -80.75 -144.95 66.19 2 90.00 100.55 37.50 2 65.41 -4.49 29.83 2 and this goes on for a A LOT more records, until time=1200 I want to create a 5 degree by 5 degree grid of this data, with the value of temperature in the appropriate grid cell. I want one grid for each time value. For each time value, this works out to be a 36x72 grid with 2592 cells because the longitude spans -180 to 180 and latitude spans 90 to -90 and they would be in increments of 5 degrees. If there are no temperatures available to be put into a grid cell, than that cell should get a missing value, NA, put into it. Also, could the gridded result for each time be written to a text file before processing the next time value? Hope this is clear. Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/gridding-values-in-a-data-frame-tp23319190p23319190.html Sent from the R help mailing list archive at Nabble.com.
dxc13 wrote:> > Hi all, > I have a data frame that looks like such: > LATITUDE LONGITUDE TEMPERATURE TIME > 36.73 -176.43 58.32 1 > > and this goes on for a A LOT more records, until time=1200 > > I want to create a 5 degree by 5 degree grid of this data, with the value > of temperature in the appropriate grid cell. I want one grid for each > time value. > > dxc13 >The following appears to work, but is most definitely *not* in the "spirit of R" -- it's more like a C program written in R. Undoubtedly someone will come up with a much more clever method. (This is always my problem; I come up with a workable solution, but it's not elegant.) Note: the grid has to have 37 rows, since latitude -90 to +90 (inclusive) takes 37 5-degree increments; since -180 and +180 longitude are the same, you would never have them as separate numbers. d <- read.csv("weather.csv") for (i in 1:1200) { x <- d[d$TIME==i,] if (length(x$TIME) > 0) { print(sprintf("# of elements for time %d: %d", i, length(x$TIME))) grid <- matrix(NA, nrow=37, ncol=72) for (j in 1:length(x$TIME)) { lat <- 1 + trunc((90 + x$LATITUDE[j]) / 5) long <- 1 + trunc((180 + x$LONGITUDE[j]) / 5) grid[lat,long] <- x$TEMPERATURE[j] } write(t(grid), file=sprintf("time%d.csv", i), ncolumns=72, sep=",") } } -- View this message in context: http://www.nabble.com/gridding-values-in-a-data-frame-tp23319190p23325204.html Sent from the R help mailing list archive at Nabble.com.
It's hard to check without a reproducible example, but the following code should give you a 3d array of lat x long x time: library(reshape) df$lat <- round_any(df$LATITUDE, 5) df$long <- round_any(df$LONGITUDE, 5) df$value <- df$TIME cast(df, lat ~ long ~ time, mean) On Thu, Apr 30, 2009 at 10:55 AM, dxc13 <dxc13 at health.state.ny.us> wrote:> > Hi all, > I have a data frame that looks like such: > LATITUDE ? LONGITUDE ? TEMPERATURE ? TIME > 36.73 ? ? ? ? -176.43 ? ? ? ?58.32 ? ? ? ? ? ? ? 1 > 50.95 ? ? ? ? ? ?90.00 ? ? ? ?74.39 ? ? ? ? ? ? ? 1 > -30.42 ? ? ? ? ? ?5.45 ? ? ? ?23.26 ? ? ? ? ? ? ? 1 > 15.81 ? ? ? ? -109.31 ? ? ? ?52.44 ? ? ? ? ? ? ? 1 > -80.75 ? ? ? ?-144.95 ? ? ? ?66.19 ? ? ? ? ? ? ?2 > 90.00 ? ? ? ? ?100.55 ? ? ? ?37.50 ? ? ? ? ? ? ? 2 > 65.41 ? ? ? ? -4.49 ? ? ? ? ? 29.83 ? ? ? ? ? ? ? 2 > > and this goes on for a A LOT more records, until time=1200 > > I want to create a 5 degree by 5 degree grid of this data, with the value of > temperature in the appropriate grid cell. ?I want one grid for each time > value. ?For each time value, this works out to be a 36x72 grid with 2592 > cells because the longitude spans -180 to 180 and latitude spans 90 to -90 > and they would be in increments of 5 degrees. ?If there are no temperatures > available to be put into a grid cell, than that cell should get a missing > value, NA, put into it. > Also, could the gridded result for each time be written to a text file > before processing the next time value? > > Hope this is clear. > Thanks in advance. > > dxc13 > -- > View this message in context: http://www.nabble.com/gridding-values-in-a-data-frame-tp23319190p23319190.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- http://had.co.nz/