# [R-sig-Geo] R: suggestion to write a more elegant code

Alessandro alessandro.montaghi at unifi.it
Fri Oct 17 02:56:05 CEST 2008

```Hi Thanx Alex and Mike

SORRY I am a brand-new

I tried this formula

for (pow in seq(1.5, 4.0, by = 0.1))
rsaga.grid.calculus(in.grids = c("DCM_1.sgrd"),
out.grid = "DCM_1_power15.sgrd",
formula = paste("a^", pow, sep=""))

but the code create only one raster (DCM_1_power15.sgrd)

with this formula I have this error

power<- seq(1.5, 4.0, by=0.1)
for (i in 1:length(power)){
rsaga.grid.calculus(in.grids = c("DCM_1.sgrd"),out.grid
=paste("DCM_1_power"power[i]*10,".sgrd",sep=""), formula = ~^power[i])
}

ERROR message

power<- seq(1.5, 4.0, by=0.1)
> for (i in 1:length(power)){
+ rsaga.grid.calculus(in.grids = c("DCM_1.sgrd"),out.grid
=paste("DCM_1_power"power[i]*10,".sgrd",sep=""), formula = ~^power[i])
Errore: unexpected symbol in:
"for (i in 1:length(power)){
rsaga.grid.calculus(in.grids = c("DCM_1.sgrd"),out.grid
=paste("DCM_1_power"power"
> }
Errore: unexpected '}' in "}"
>
>

-----Messaggio originale-----
Da: Alexander Brenning [mailto:brenning at uwaterloo.ca]
Inviato: giovedì 16 ottobre 2008 17.01
A: Alessandro
Cc: r-sig-geo at stat.math.ethz.ch
Oggetto: Re: [R-sig-Geo] suggestion to write a more elegant code

Hi,

(and in your case only) grid in in.grids.

Instead of using a formula, you can use a character string:
formula = "a^1.5" etc.,
so you just have to 'paste' things together; this should work:

for (pow in seq(1.5, 4.0, by = 0.1))
rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),
out.grid = "DCM_1_power15.sgrd",
formula = paste("a^", pow, sep=""))

Cheers
Alex

Alessandro wrote:
> Hi all,
>
>
>
> I need a suggestion to write this code in R more elegant . I have a  DSM
> grid and I need to create a different grid raisin to different power.
>
>
>
>
>
> Thanx Ale
>
>
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^1.5)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power16.sgrd", formula = ~^1.6)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power17.sgrd", formula = ~^1.7)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power18.sgrd", formula = ~^1.8)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power19.sgrd", formula = ~^1.9)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.0)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power20.sgrd", formula = ~^2.1)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.2)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.3)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.4)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.5)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^1.6)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.7)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.8)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^2.9)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.0)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.1)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.2)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.3)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.4)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.5)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.6)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.7)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.8)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^3.9)
>
> rsaga.grid.calculus(in.grids = c("DSM_1.sgrd"),out.grid =
> "DCM_1_power15.sgrd", formula = ~^4.0)
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

--
Alexander Brenning
brenning at uwaterloo.ca - T +1-519-888-4567 ext 35783
Department of Geography and Environmental Management
University of Waterloo
200 University Ave. W - Waterloo, ON - Canada N2L 3G1
http://www.fes.uwaterloo.ca/geography/faculty/brenning/

```