[R-sig-Geo] several 'if' conditions with 'apply'
Crowe, Andrew
a.crowe at lancaster.ac.uk
Thu Jun 5 17:12:07 CEST 2008
Michal
All the if statements are evaluated in your code, so the value of xx is assigned by the final if statement. This should be solved by using 'else if' so that the code drops out of the else block when the value is assigned.
Regards
Andrew
Dr Andrew Crowe
Lancaster Environment Centre
Lancaster University
Lancaster LA1 4YQ
UK
Tel: +44 (0)1524 595879
________________________________
From: r-sig-geo-bounces at stat.math.ethz.ch on behalf of Michal Gallay
Sent: Thu 05/06/2008 2:44 PM
To: r-sig-geo at stat.math.ethz.ch
Subject: [R-sig-Geo] several 'if' conditions with 'apply'
Dear R Users,
I am trying to apply a function to each cell of a data frame, which can
contain NAs, or values between -360 to +360.
The function should test if each value fullfils one of the conditions and
converts this value accordingly.
As can be seen from the outputs it seems that only the last 'if' condition
in the 'else' block is applied so that only some values were correctly
transformed.
I cannot really get my head around this, what I am doing wrong. I tried
several versions. I feel I am missing something very simple but I just
can't see it.
Would some be able to help please?
Thank you very much for any directions of how to approach it.
Michal
> resid
a b c d e f
7325 270 10 0 4 7 107
7326 NA NA NA NA NA NA
7327 NA NA NA NA NA NA
7328 212 330 0 321 -1 -1
7329 3 5 0 22 -292 -215
7330 353 345 0 317 359 126
>
> my.fun <- function(xx)
+ {
+
+ if(is.na(xx)) # if the value at xx is NA, let it be otherwise ...
+ {
+ xx=xx
+ } else
+ {
+ if(xx < 180 & xx > 0 || xx == 180 || xx == -180 || xx ==0) xx=abs(xx);
+ if(xx > -180 & xx <0) xx=xx;
+ if(xx > 180 & xx < 360) xx=xx-360;
+ if(xx < -180 & xx > -360) xx=xx+360
+ }
+ }
>
>
> bb <- apply(resid, c(1,2), my.fun)
> bb
a b c d e f
7325 NULL NULL NULL NULL NULL NULL
7326 NA NA NA NA NA NA
7327 NA NA NA NA NA NA
7328 NULL NULL NULL NULL NULL NULL
7329 NULL NULL NULL NULL 68 145
7330 NULL NULL NULL NULL NULL NULL
>
PS:
> sessionInfo()
R version 2.6.2 (2008-02-08)
i386-pc-mingw32
locale:
LC_COLLATE=Slovak_Slovakia.1250;LC_CTYPE=Slovak_Slovakia.1250;LC_MONETARY=Slovak_Slovakia.1250;LC_NUMERIC=C;LC_TIME=Slovak_Slovakia.1250
attached base packages: [1] stats graphics grDevices utils datasets methods
base other attached packages: [1] circular_0.3-8 boot_1.2-31 rgdal_0.5-24
sp_0.9-23 loaded via a namespace (and not attached): [1] grid_2.6.2
lattice_0.17-6 rcompgen_0.1-17 tools_2.6.2
>
--
Michal Gallay
Postgraduate Research Student
School of Geography, Archaeology and Palaeoecology
Queen's University
Belfast BT7 1NN
Northern Ireland
Tel: +44(0)2890 273929
Fax: +44(0)2890 973212
email: mgallay01 at qub.ac.uk
www: www.qub.ac.uk/geog
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list