[R] gridding values in a data frame
dxc13
dxc13 at health.state.ny.us
Fri May 1 00:22:11 CEST 2009
Thank you for your input. I will give it a try and see how it works out. I
always have the same problem when programming...I always make things more
complicated than they really are :-).
Much appreciated.
jdeisenberg wrote:
>
>
>
> 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-tp23319190p23325394.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list