[R] how to skip part of the code

arun smartpink111 at yahoo.com
Wed Mar 20 16:07:58 CET 2013



Hi,

set.seed(24)
 dat1<- data.frame(a=1:5,b=2:6,e=sample(c(0,1,2),5,replace=TRUE))
merge(within(subset(dat2,e>0),d<-a+b),within(subset(dat2,e==0),f<-a-b),all=TRUE)
#  a b e  d  f
#1 1 2 0 NA -1
#2 2 3 0 NA -1
#3 3 4 2  7 NA
# 4 5 1  9 NA
#5 5 6 1 11 NA
A.K.



----- Original Message -----
From: Andras Farkas <motyocska at yahoo.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, March 20, 2013 9:11 AM
Subject: [R] how to skip part of the code

Dear All,
 
another quick question, this one is on skipping part of my code, so let us say:
 
a <-5
b <-2
e <-0
 
d <-a+b
f <-a-b
 
what I would like to do is to have R NOT to calculate the value for d in case the value of e equals to zero (essentially skip that "chunk"), but instead move on to calculate te value for f. In the code I am working with the value of e changes, and I would like to calculate d and f at all times when the value of e is greater then zero. If possible, I would like to do this without using the functions "ifelse" and "if else"
 
appreciate the help,
 
Andras
    [[alternative HTML version deleted]]


______________________________________________
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.




More information about the R-help mailing list