[R] Retain parts of a matrix

Jim Lemon jim at bitwrit.com.au
Mon Nov 28 09:41:25 CET 2011


On 11/28/2011 04:06 PM, Katrina Bennett wrote:
> Sorry for not being more clear.
>
> I'll try to explain again.
>
> I have a rather large DEM and I need to scale daily temperature values for
> 10 years.
>
> I am using the sapply function to adjust temperatures (t.mean.1.c) based on
> lapse rates across DEM points (cdem) which meet the condition of elevation
> being greater than 300m. Below ~300m the lapse rate calculate changes
> because temperatures decrease with elevation. Above ~300m the lapse rates
> calculation must account for an inversion, where temperature increases with
> elevation.
>
> What I would like as a result is the values of the DEM which are lower than
> 300m to retain a generic lapse rate adjustment, and the DEM values great
> than 300 to be treating using a different function.
>
>...
Hi Katrina,
If cdem is a matrix (and it looks like a vector in your example), you 
could try:

le300<-function(elevation,temp) return(temp+elevation*0.0065)
gt300<-function(elevation,temp) return(temp-elevation*0.0065)
cdem[cdem<=300]<-le300(cdem[cdem<=300],t.mean.1.c[cdem<=300])
cdem[cdem>300]<-gt300(cdem[cdem>300],t.mean.1.c[cdem>300])

where le300 is the correction for altitudes less than or equal to 300M 
and gt300 is the one for altitudes greater than 300M. This works for me 
with a toy function. However, I don't get the same numbers as you and 
wonder if your function is doing the same as the ones I use above. Also, 
why 300m in the text and 900m in the functions? Are we mixing up feet 
and meters?

Jim



More information about the R-help mailing list