Hi David and Jim!
Thanks very much for your help, this worked for me.
#generate two matrices that are the same size for elevation and temperature:
t.mean.1.c <- matrix(c(-15, -20, -30, -20, -25, -35, -40, -8, 9, 10),
nrow=10, ncol=15, byrow=F)
cdem <- matrix(c(300, 400, 700, 900, 1000, 250, 200, 300, 500, 650, 650,
1200, 1400, 3200, 2000),nrow=nrow(t.mean.1.c),
ncol=15, byrow=T)
#create the functions to use:
gt300 <- function(elevation,temp) return((elevation * 0.0065) + temp)
lt300 <- function(elevation,temp) return((elevation * -0.0065) + temp)
#apply the function and subset everything by the values I want to apply it
to
cdem[cdem>=300] <- gt300(cdem[cdem>=300],t.mean.1.c[cdem>=300])
cdem[cdem<300] <- lt300(cdem[cdem<300],t.mean.1.c[cdem<300])
This retains the matrix and scales everything properly.
Thanks again!
Katrina
p.s. Jim - yes, the dummy subset value I was using 900m but I switched it
in my example to my actual value (since I am going to use 300m for this
initial attempt at this). I will need to make it a lot more complicated
eventually!
On Mon, Nov 28, 2011 at 6:57 PM, Katrina Bennett
<kebennett@alaska.edu>wrote:
> Hi David and Jim!
>
> Thanks very much for your help, this worked for me.
>
> #generate two matrices that are the same size for elevation and
> temperature:
> t.mean.1.c <- matrix(c(-15, -20, -30, -20, -25, -35, -40, -8, 9, 10),
> nrow=10, ncol=15, byrow=F)
> cdem <- matrix(c(300, 400, 700, 900, 1000, 250, 200, 300, 500, 650, 650,
> 1200, 1400, 3200, 2000),nrow=nrow(t.mean.1.c),ncol=15, byrow=T)
>
> #create the functions to use:
> gt300 <- function(elevation,temp) return(temp + (elevation * 0.0065))
> lt300 <- function(elevation,temp) return(temp + (elevation * -0.0065))
>
> #apply the function and subset everything by the values I want to apply it
> to
> cdem[cdem>=300] <- gt300(cdem[cdem>=300],t.mean.1.c[cdem>=300])
> cdem[cdem<300] <- lt300(cdem[cdem<300],t.mean.1.c[cdem<300])
>
> This retains the matrix and scales everything properly.
>
> Thanks again!
>
> Katrina
>
> p.s. Jim - yes, the dummy subset value I was using 900m but I switched it
> in my example to my actual value (since I am going to use 300m for this
> initial attempt at this). I will need to make it a lot more complicated
> eventually!
>
>
>
>
> On Sun, Nov 27, 2011 at 11:41 PM, Jim Lemon <jim@bitwrit.com.au>
wrote:
>
>> 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
>>
>
>
>
> --
> Katrina E. Bennett
> PhD Student
> University of Alaska Fairbanks
> International Arctic Research Center
> 930 Koyukuk Drive, PO Box 757340
> Fairbanks, Alaska 99775-7340
> 907-474-1939 office
> 907-385-7657 cell
> kebennett@alaska.edu
>
>
> Personal Address:
> UAF, PO Box 752525
> Fairbanks, Alaska 99775-2525
> bennett.katrina@gmail.com
>
>
--
Katrina E. Bennett
PhD Student
University of Alaska Fairbanks
International Arctic Research Center
930 Koyukuk Drive, PO Box 757340
Fairbanks, Alaska 99775-7340
907-474-1939 office
907-385-7657 cell
kebennett@alaska.edu
Personal Address:
UAF, PO Box 752525
Fairbanks, Alaska 99775-2525
bennett.katrina@gmail.com
[[alternative HTML version deleted]]