Much easier: use ifelse (the vectorized function) instead as follows: ifelse(A < 2, ifelse(A < 1, 2, 1), 0)
But you could probably just do 2 - A in this case which would be easiest. Michael On Sun, Feb 5, 2012 at 5:30 PM, Philip Robinson <philip.c.robin...@gmail.com> wrote: > I have a vector of 2,1,0 I want to change to 0,1,2 respectively (the data > is allele dosages) > > I have tried multiple nested if/else statements and looked at the ?if help > and cannot work out what is wrong, other people have posted code which is > identical and they state works. > > Any help would be greatly appreciated. > >> A[1:20] > [1] 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0 > >> B <- rep(NA,length(A)) > >> for (i in 1:length(A)){ if(A[i]==2){B[i] <- 0} else > + if(A[i]==0){B[i] <- 2} else > + if(A[i]==1){B[i] <- 1}} > > Error in if (A[i] == 2) { : missing value where TRUE/FALSE needed > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.