From: Rolf Turner <r.turner_at_auckland.ac.nz>

Date: Wed, 04 Jun 2008 14:05:53 +1200

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 04 Jun 2008 - 02:58:27 GMT

Date: Wed, 04 Jun 2008 14:05:53 +1200

This is an FAQ (although it doesn't seem to be listed in the official
FAQs.

The ``if'' command compares scalars (only, in effect).

See ?"if"

You need to use the ifelse() function:

folds <- function(x,y) ifelse(x-y>=0,2^(x-y),-2^(y-x))

This if for illustrative purposes only. The foregoing would be better done as:

folds <- function(x,y) sign(x-y)*2^abs(x-y)

cheers,

Rolf Turner

On 4/06/2008, at 1:47 PM, ALAN SMITH wrote:

> Hello R users and developers,

*> I am trying to write several functions (fairly new at this) in order
**> to avoid using loops on large data frames (because they are slow). I
**> wrote the function below and it seems to get hung on the first part of
**> the if statement and then applies that condition to rest of the
**> function. So if (x-y) is greater than 0 the function uses the true
**> statement for the calculations. Could someone please offer some
**> advise on how to write these functions a little better or a type
**> "apply" that I may use with two (or more) different vectors of data
**> required by a single functions.
**> ####################### Examples
**> #######################################################
**> ## example 1 ###
**> x<-c(5,6,4,3,5,3,1)
**> y<-c(1,6,2,1,7,1,9)
**> folds<-function(x,y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})
**> z<-folds(x,y)
**> check<-cbind(x,y,z)
**> View(check)
**>
**> Warning message:
**> In if ((x - y) >= 0) { :
**> the condition has length > 1 and only the first element will be used
**> ### why will it only use the first element and how to I get around
**> this ####
**>
**> ## example 2 making the fist comparison negative ###
**> x1<-c(5,6,4,3,5,3,1)
**> y1<-c(11,6,2,1,7,1,9)
**> folds<-function(x,y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})
**> z1<-folds(x1,y1)
**> check2<-cbind(x1,y1,z1)
**> View(check2)
**> Warning message:
**> In if ((x - y) >= 0) { :
**> the condition has length > 1 and only the first element will be used
**> ######################################################################
**> ##########
**> #### loop I am trying to avoid writing many many times #####
**> folds2<-NULL
**> xy<-as.data.frame(cbind(x,y))
**> for (i in 1:nrow(xy)) {
**> diff<-xy$x[i]-xy$y[i]
**> folds2[i]<-if(diff>=0) {2^diff} else{-(2^abs(diff))}
**> }
**> xyz<-cbind(xy,folds2)
**> View(xyz)
**> #################
**>
**> Thank you,
**> Alan
**>
**> P.S. why does ?function not work
**>
**> ______________________________________________
**> R-help_at_r-project.org mailing list
**> https://stat.ethz.ch/mailman/listinfo/r-help
**> PLEASE do read the posting guide http://www.R-project.org/posting-
**> guide.html
**> and provide commented, minimal, self-contained, reproducible code.
*

######################################################################Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 04 Jun 2008 - 02:58:27 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Wed 04 Jun 2008 - 04:30:38 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*