[R] if else does not return right value

Muhammad Rahiz muhammad.rahiz at ouce.ox.ac.uk
Sat Dec 26 20:03:03 CET 2009


Thanks David and Patrick,

I need to use two statements for the if condition specified. Since the 
first statement feeds into the second, the ifelse function may just work 
for the dataset. I'm not sure if there will be issues later but I've 
tested it and it works.

x <- as.matrix(read.table("test.txt"))

Original code;

if (x < 20){
y <- x + 100 # statement 1a
... # statement 1b
} else {
y <- x -100 # statement 2a
... # statement 2b
}

New code:

n0 <- x + 100
n1 <- n0 + 0

m0 <- x - 100
m1 <- m0 - 0

ifelse(x < 20, n1, m1)


Muhammad

Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling					
Climate Research Laboratory, School of Geography & the Environment	
Oxford University Centre for the Environment
South Parks Road, Oxford, OX1 3QY, United Kingdom 
Tel: +44 (0)1865-285194	 Mobile: +44 (0)7854-625974
Email: muhammad.rahiz at ouce.ox.ac.uk




More information about the R-help mailing list