Dear list, I get a syntax error for the following function: for(na.omit(sqrt(D))>2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C))))} Any idea what is wrong? I am using R 1.8.1 on Linux, Kernel 2.4.21-i686. Regards, Ulrich -- __________________________________________________ Ulrich Leopold MSc. Computational Bio- and Physical Geography (CBPG) Institute for Biodiversity and Ecosystem Dynamics (IBED) Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Room: B2.52 Phone: +31 20 525 7456 (7451 Secretary) Fax: +31 20 525 7431 Mobile: +31 64 220 3028 Email: uleopold at science.uva.nl URL: www.science.uva.nl/ibed/cbpg
Ulrich Leopold <uleopold at science.uva.nl> writes:> Dear list, > > I get a syntax error for the following function: > > for(na.omit(sqrt(D))>2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C))))} > > Any idea what is wrong?Yes, that's not the syntax for a for loop. I have no idea what your intention might have been, though. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Ulrich Leopold wrote:> Dear list, > > I get a syntax error for the following function: > > for(na.omit(sqrt(D))>2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C))))} > > Any idea what is wrong? > > I am using R 1.8.1 on Linux, Kernel 2.4.21-i686. > > Regards, Ulrich >`na.omit(sqrt(D))>2' is not of the form `var in seq' as the help page for `for' would have told you. Try help("for"). --sundar
Ulrich Leopold wrote:>Dear list, > >I get a syntax error for the following function: > >for(na.omit(sqrt(D))>2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C))))} > >Any idea what is wrong? > >I am using R 1.8.1 on Linux, Kernel 2.4.21-i686. > >Regards, Ulrich > > >have a look at help("for"): Control package:base R Documentation Control Flow Description: ... Usage: if(cond) expr if(cond) cons.expr else alt.expr for(var in seq) expr ... Examples: for(i in 1:5) print(1:i) for(n in c(2,5,10,20,50)) { x <- rnorm(n) cat(n,":", sum(x^2),"\n") } Peter Wolf
Dear Ulrich, I believe that you want ifelse(), not if(), but there are likely some other problems lurking here: You're removing missing data independently from the several vectors -- unless the NAs are all in the same places, things will not work or not work correctly. As well, what do you want to happen if the condition fails? John> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ulrich Leopold > Sent: Wednesday, May 05, 2004 10:06 AM > To: R-help > Subject: [R] syntax error in function 'for' > > Dear list, > > I get a syntax error for the following function: > > for(na.omit(sqrt(D))>2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C))))} > > Any idea what is wrong? > > I am using R 1.8.1 on Linux, Kernel 2.4.21-i686. > > Regards, Ulrich > > --
Hi On 5 May 2004 at 11:53, John Fox wrote:> Dear Ulrich, > > I believe that you want ifelse(), not if(), but there are likely some > other problems lurking here: You're removing missing data > independently from the several vectors -- unless the NAs are all in > the same places, things will not work or not work correctly. As well, > what do you want to happen if the condition fails?maybe complete.cases() is more appropriate but it also depends on what is A,B,C D and what is your real intention. e.g.> A<-1:10 > B<-11:20 > C<-101:110 > D<-runif(10)*10 > D[1] 3.834122 7.422188 2.741723 2.065324 2.775822 6.991390 1.899406 7.741244 7.026734 3.417432> A[5]<-NA > C[8]<-NA > D[2]<-NA > fff<-function(a,b,c) a-b+sqrt(c) > na.omit(subset(fff(A,B,C), sqrt(D)>2))[1] 0.2956301 0.4403065 attr(,"na.action") [1] 2 attr(,"class") [1] "omit" Cheers Petr> > John > > > -----Original Message----- > > From: r-help-bounces at stat.math.ethz.ch > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ulrich > > Leopold Sent: Wednesday, May 05, 2004 10:06 AM To: R-help Subject: > > [R] syntax error in function 'for' > > > > Dear list, > > > > I get a syntax error for the following function: > > > > for(na.omit(sqrt(D))>2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C))))} > > > > Any idea what is wrong? > > > > I am using R 1.8.1 on Linux, Kernel 2.4.21-i686. > > > > Regards, Ulrich > > > > -- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz