G'day Everyone,

I spent a few weeks struggling with R on this issue and gave up because I was frustrated. Here is my issues:

I have a large data set with about 125,000 presence/absence records across Australia over a 4 year period. I have also downloaded daily raster files for rain, max and min temperature and solar radiation from the Australian Bureau of Meteorology (BOM) for this 4 year period and the one year before. I would like to produce a matrix that has each presence/absence record with corresponding daily whether data for 6 months before the record was taken.

To go about this I tried extracting raster values from points so I would have a big data set where each record has an extracted value for every day. I could use this matrix of data to select the relevant records I want. I Tried to do this by extracting all 2000+ rasters, which didn't work. I then tried it in blocks of 500 rasters, which worked, but R struggled to open the large files, when I wanted to join them together. I asked for help on stack overflow, someone kindly provided the following code:

files <- list(bom_rasters)
library(sp)
data<- read.table("presence_absence_data.csv", header=TRUE, sep=',')
SP <- SpatialPoints(data[,1:2])

library(raster)
for (i in seq_len(length(files))) {

    r <- raster(files[[i]])
    e <- extract(r, SP)
    ## print(e)  ## print for debugging
    write.table(data.frame(file = i, extract = e),"cellSummary.csv", col.names = i == 1, append = i > 1, sep = ",", row.names = FALSE)
}

This code almost does what I want. It reads each raster separately and prints out the extracted values for each point. However, it prints each new extracted values in the same columns as the previous raster so I end up with a very long data set which stops at the max number of rows. Can anyone help with making this code write each new set of extracted values into the next row along so it looks like

                X             Y              raster1  raster2  raster3
                145         34           10           11           9
                146         33           20           22           26

I have tried to write some code that prints a new column that mimics the header for each raster values column, which I intend to use to look up the appropriate data. I used the following code but it would not print anything with the same length as the dataset which is 125,000 rows long. My data looked like

                Date                      yr            mth        day         X19980101           X19980102           X19980103
                1999/01/01         1999       01           01           10                           10                           11

X1<-read.table(file.choose(), header=TRUE, sep=',')

for (i in seq_len(length(X1$date))) {
    new.dates = paste('X',X1$yr[i],X1$mth[i], X1$day[i], sep='',")
    print(new.dates)
}

I am really new to all this looping stuff and trying to make things work with less code. I am not sure where I have gone wrong here but it doesn't seem to work.

Sorry about this essay, but I have been trying and am about ready to give up. I thought I should try R-sig-geo before I do. Any help would be greatly appreciated. Thanks heaps in advance!


Kind regards,

Adam Cardilini
PhD Candidate
Schools of Life and Environmental Sciences,
Deakin University, 75 Pigdons Rd,
Waurn Ponds, Vic, Australia, 3217
Mob: 0431 566 340
Email: apcar@deakin.edu.au


	[[alternative HTML version deleted]]

