[R-sig-Geo] Automate the extraction of climate variable at different time depths from netcdf in r

Barnabas Daru darunabas at gmail.com
Sun Oct 12 00:46:04 CEST 2014


Hi Robert,
Thank you very much for the code. It works.
Thanks and kind regards
Barnabas Daru


  \-/
   /\
  /--|    
 /---/ Barnabas Daru
 |--/  PhD Candidate,
 \-/   African Centre for DNA Barcoding,
 /\    University of Johannesburg,
/--\   PO Box 524, Auckland Park, 2006,
|---\  Johannesburg, South Africa.
 \---\ Lab: +27 11 559 3477        
  \--| Mobile: +277 3818 9583
   \-/ Website: www.barnabasdaru.com
   /\        
  /--\ 
 
#…if you can think it, you can do it.
 




On Sep 11, 2014, at 6:47 PM, Robert J. Hijmans wrote:

> Barnabas,
> I doubt you will need to "delete one-by-one, the values for the years
> that I do not desire". There surely are basic R tricks for that.
> Perhaps the below example can get you there:
> 
> ## example data
> #raster
> b <- brick(system.file("external/rlogo.grd", package="raster"))
> # points
> set.seed(0)
> p <- xyFromCell(b, sample(ncell(b), 10))
> # "years"
> y <- cbind(y1=sample(0:1, 10, replace=TRUE), y2=sample(0:1, 10,
> replace=TRUE), y3=sample(0:1, 10, replace=TRUE))
> 
> 
> # extract all
> e <- extract(b, p)
> # combine data
> x <- data.frame(x=p[,1], y=p[,2], year=rep(1:3, each=nrow(p)),
> include=as.vector(y), value=as.vector(e))
> # select values you want
> x[x$include==1, ]
> 
> Whether the above works for you really depends on how you need to get
> the values for further processing.
> 
> Robert
> 
> 
> 
> On Sun, Sep 7, 2014 at 8:58 AM, Barnabas Daru <darunabas at gmail.com> wrote:
>> Hi Robert,
>> Thanks very much for your suggestion.
>> I tried the code you provided and it work! Many thanks.
>> However, it tends to extract the SST values for all the points and for time
>> periods including even the years that I don't desire. This means I will have
>> to delete one-by-one, the values for the years that I do not desire and keep
>> only the ones I want.
>> I thought there is a way I could simply ask R to print in one column, the
>> extracted values associated with only the years for which point data is
>> available.
>> Thanks and kind regards
>> Barnabas
>> 
>> 
>> 
>>  \-/
>>   /\
>>  /--|
>> /---/ Barnabas Daru
>> |--/  PhD Candidate,
>> \-/   African Centre for DNA Barcoding,
>> /\    University of Johannesburg,
>> /--\   PO Box 524, Auckland Park, 2006,
>> |---\  Johannesburg, South Africa.
>> \---\ Lab: +27 11 559 3477
>>  \--| Mobile: +277 3818 9583
>>   \-/ Website: www.barnabasdaru.com
>>   /\
>>  /--\
>> 
>> #…if you can think it, you can do it.
>> 
>> 
>> 
>> 
>> 
>> On Sep 7, 2014, at 5:23 AM, Robert J. Hijmans wrote:
>> 
>> Barnabas, ,
>> You can try something like this:
>> 
>> b <- brick("~sst.mnmean.nc", varname="sst")
>> # loop over species, or combine all species into one data.frame?
>> mydata <- read.csv("~Species one.csv")
>> extract.mydata <- extract(b, mydata[,5:6])
>> write.csv(extract.mydata, file = "Species_one_extracted.csv")
>> 
>> Robert
>> 
>> 


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list