Re: [R] ifelse question
...ifelse, a function of three **vector** arguments Yes !! I misunderstood the functioning of ifelse. Thanks Jacques. Peter Dalgaard wrote: > [EMAIL PROTECTED] wrote: >> What is puzzling me is that rnorm(1) is only evaluated *twice*, one >> time for each branch, with only 2 different random deviates, instead >> of giving ten different random deviates. y1 has indeed 10 values but >> with only 2 different ones. >> > I find it more puzzling why you expect that ifelse, a function of > three vector arguments, would cause its input arguments to be > reevaluated for every element of the result. > >> I would like to have rnorm be evaluated for each row and collect ten >> *different* random deviates. >> >> y1 >> [1] 0.4087172 0.7707796 0.4087172 0.4087172 0.7707796 0.4087172 >> 0.4087172 >> [8] 0.7707796 0.7707796 0.4087172 >> >> >> Thanks. >> >> Jacques. >> >> __ >> R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] ifelse question
I think you can find your answer if you study this part of the documentation for ifelse: Details: If yes or no are too short, their elements are recycled. yes will be evaluated if and only if any element of test is true, and analogously for no. Also, consider this call: ifelse(1:12 > 5, 1:3, 11:14) -- Tony Plate Jacques Ropers wrote: >>But you got only two (eventually one) distinct values, right? Look at >>the code for 'ifelse': yes and no are only called once each, then >>recycled to desired length. >> >>I guess you want something like >> >>x <- rnorm(10) >>y <- rnorm(10) >>z <- rnorm(10) >>y1 <- ifelse(x > 0, y, z) >> > > Thanks for the help. > > Although this would do the trick, is there a way to call repetitively > rnorm (rpois...) *inside the ifelse* rather than constructing the vector > outside ? Like in the following where cos() and sin() functions are > evaluated for each row : > x <- rnorm(10) > y1 <- ifelse(x > 0, cos(x), sin(x)) > > I am trying to understand the difference of behaviour. R acts as if > rnorm(1) return value were known after the first call and does not > evaluate rnorm(1) in > > y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) > > again after the first evaluation. > > > Jacques. > > __ > R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] ifelse question
[EMAIL PROTECTED] wrote: > What is puzzling me is that rnorm(1) is only evaluated *twice*, one time > for each branch, with only 2 different random deviates, instead of > giving ten different random deviates. y1 has indeed 10 values but with > only 2 different ones. > I find it more puzzling why you expect that ifelse, a function of three vector arguments, would cause its input arguments to be reevaluated for every element of the result. > I would like to have rnorm be evaluated for each row and collect ten > *different* random deviates. > > y1 > [1] 0.4087172 0.7707796 0.4087172 0.4087172 0.7707796 0.4087172 0.4087172 > [8] 0.7707796 0.7707796 0.4087172 > > > Thanks. > > Jacques. > > __ > R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] ifelse question
> But you got only two (eventually one) distinct values, right? Look at > the code for 'ifelse': yes and no are only called once each, then > recycled to desired length. > > I guess you want something like > > x <- rnorm(10) > y <- rnorm(10) > z <- rnorm(10) > y1 <- ifelse(x > 0, y, z) > Thanks for the help. Although this would do the trick, is there a way to call repetitively rnorm (rpois...) *inside the ifelse* rather than constructing the vector outside ? Like in the following where cos() and sin() functions are evaluated for each row : x <- rnorm(10) y1 <- ifelse(x > 0, cos(x), sin(x)) I am trying to understand the difference of behaviour. R acts as if rnorm(1) return value were known after the first call and does not evaluate rnorm(1) in y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) again after the first evaluation. Jacques. __ R-help@stat.math.ethz.ch 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.
Re: [R] ifelse question
On 12/12/06, Leeds, Mark (IED) <[EMAIL PROTECTED]> wrote: > ifelse is vectorized but there is no way you could know what's > happening with that command > because you have rnorm(1) for both conditions. I think you mean to have > something different in one of them ? > > Whewn I run your code in my R session, I get 10 values for y1, so there > isn't anything wrong except > That you have the same statement for both cases. But you got only two (eventually one) distinct values, right? Look at the code for 'ifelse': yes and no are only called once each, then recycled to desired length. I guess you want something like x <- rnorm(10) y <- rnorm(10) z <- rnorm(10) y1 <- ifelse(x > 0, y, z) but this whole business is probabilistically the same as x <- rnorm(10) y1 <- rnorm(10) which of course is faster and more transparent. Göran > > > > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > [EMAIL PROTECTED] > Sent: Tuesday, December 12, 2006 3:13 PM > To: r-help@stat.math.ethz.ch > Subject: [R] ifelse question > > Dear R-helpers, > How come that in the following code the rnorm() function is evaluated > only once for each branch of the 'ifelse' ? > > x <- rnorm(10) > y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) > > > What is the right way to make it called/evaluated for each row, apart > from a 'for' loop ? > > Thanks, > > Jacques. > > __ > R-help@stat.math.ethz.ch 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. > > > This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} > > __ > R-help@stat.math.ethz.ch 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. > -- Göran Broström __ R-help@stat.math.ethz.ch 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.
Re: [R] ifelse question
What is puzzling me is that rnorm(1) is only evaluated *twice*, one time for each branch, with only 2 different random deviates, instead of giving ten different random deviates. y1 has indeed 10 values but with only 2 different ones. I would like to have rnorm be evaluated for each row and collect ten *different* random deviates. y1 [1] 0.4087172 0.7707796 0.4087172 0.4087172 0.7707796 0.4087172 0.4087172 [8] 0.7707796 0.7707796 0.4087172 Thanks. Jacques. __ R-help@stat.math.ethz.ch 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.
Re: [R] ifelse question
ifelse is vectorized but there is no way you could know what's happening with that command because you have rnorm(1) for both conditions. I think you mean to have something different in one of them ? Whewn I run your code in my R session, I get 10 values for y1, so there isn't anything wrong except That you have the same statement for both cases. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Tuesday, December 12, 2006 3:13 PM To: r-help@stat.math.ethz.ch Subject: [R] ifelse question Dear R-helpers, How come that in the following code the rnorm() function is evaluated only once for each branch of the 'ifelse' ? x <- rnorm(10) y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) What is the right way to make it called/evaluated for each row, apart from a 'for' loop ? Thanks, Jacques. __ R-help@stat.math.ethz.ch 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. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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] ifelse question
Dear R-helpers, How come that in the following code the rnorm() function is evaluated only once for each branch of the 'ifelse' ? x <- rnorm(10) y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) What is the right way to make it called/evaluated for each row, apart from a 'for' loop ? Thanks, Jacques. __ R-help@stat.math.ethz.ch 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] ifelse question
Dear R-helpers, How come that in the following code the rnorm() function is evaluated only once for each branch of the 'ifelse' ? x <- rnorm(10) y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) What is the right way to make it called/evaluated for each row, apart from a 'for' loop ? Thanks, Jacques. __ R-help@stat.math.ethz.ch 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.
Re: [R] ifelse() question
Many thanks to Peter, Sundar and Adaikalavan for their useful help. As Adaikalavan correctly pointed out, the key was to pass as.character() as an argument within the ifelse() function. Here is the soution using a subset of my data: dat[1:10,] id long lat species type size 1 91029 -95.29819 46.41441 BOV FRM NA 2 13468 -95.09137 46.26823 POR FP0 3 92511 -95.29784 44.47016 BOV FRM NA 4 30312 -94.97496 44.41489 POR FTF 120 5 90275 -93.73471 44.92226 BOV FRM NA 6 38367 -95.54004 44.02396 POR FF NA 7 90460 -94.34028 44.89793 BOV FRM NA 8 38564 -93.51084 43.65327 OVI CL 75 9 33532 -93.06094 45.76132 POR FTF NA 10 11860 -95.23439 45.21244 POR FP0 test<-dat[1:10,] attach(test) test[,4]<-ifelse(species=="POR",as.character(type),as.character(species)) test id long lat species type size 1 91029 -95.29819 46.41441 BOV FRM NA 2 13468 -95.09137 46.26823 FP FP0 3 92511 -95.29784 44.47016 BOV FRM NA 4 30312 -94.97496 44.41489 FTF FTF 120 5 90275 -93.73471 44.92226 BOV FRM NA 6 38367 -95.54004 44.02396 FF FF NA 7 90460 -94.34028 44.89793 BOV FRM NA 8 38564 -93.51084 43.65327 OVI CL 75 9 33532 -93.06094 45.76132 FTF FTF NA 10 11860 -95.23439 45.21244 FP FP0 It worked! Now all the species =="POR" were replaced by the adjacent value on the column type. Thanks again! Francisco From: Adaikalavan Ramasamy <[EMAIL PROTECTED]> Reply-To: [EMAIL PROTECTED] To: F Z <[EMAIL PROTECTED]> CC: R-help <[EMAIL PROTECTED]> Subject: Re: [R] ifelse() question Date: Fri, 29 Oct 2004 18:44:06 +0100 Francisco, a more reproducible example would have helped but see if the following helps in your understanding # Create dataset df <- data.frame( type=1:5, species=LETTERS[1:5] ) df type species 11 A 22 B 33 C 44 D 55 E df[ ,2] == "E" [1] FALSE FALSE FALSE FALSE TRUE # Note that you need to coerce as.character inside ifelse attach(df) (df[ ,2] <- ifelse( species == "E", type, as.character(species) )) [1] "A" "B" "C" "D" "5" detach(df) df type species 11 A 22 B 33 C 44 D 55 5 On Fri, 2004-10-29 at 18:03, F Z wrote: > Thanks for you reply Peter. I tried using as.character and then converting > to factors but it did not work since it generates missing values for all the > dat[,4]=="POR". See: > > >dat[1:5,4] > [1] BOV POR BOV POR BOV > Levels: BOV CAP CER OVI POR > > test<-dat > > >test[,4]<-as.character(test[,4]) > >test[,5]<-as.character(test[,5]) > >test[test[,4]=="POR",4]<-test[test[,4]=="POR",5] > Error in "[<-.data.frame"(`*tmp*`, test[, 4] == "POR", 4, value = c(NA, : > missing values are not allowed in subscripted assignments of data > frames > >test[,4]<-as.factor(test[,4]) > >test[,5]<-as.factor(test[,5]) > > >test[1:5,4] > [1] BOV BOV BOV > Levels: BOV CAP CER OVI > > > Any suggestions? > > Thanks again! > > Francisco > > > >From: "Peter Alspach" <[EMAIL PROTECTED]> > >To: <[EMAIL PROTECTED]>,<[EMAIL PROTECTED]> > >Subject: Re: [R] ifelse() question > >Date: Fri, 29 Oct 2004 13:33:54 +1300 > > > > > >Francisco > > > >Did you try changing the factors to character, with as.character? > > > >Also you don't really need ifelse() for this. Something like the > >following (untested) should do it: > > > >dat[,4] <- as.character(dat[,4]) > >dat[,5] <- as.character(dat[,5]) > >dat[dat[,4]=='POR',4] <- dat[dat[,4]=='POR',5] > >dat[,4] <- as.factor(dat[,4]) > >dat[,5] <- as.factor(dat[,5]) > > > > > >Peter Alspach > > > > >>> "F Z" <[EMAIL PROTECTED]> 29/10/04 12:48:54 >>> > >Hi > > > >I have a data.frame with dim = 18638 (rows) 6 (cols) > > > >names(dat) > >[1] "id" "long""lat" "species" "type""size" > > > >Variable "species" and "type" are factors. Species has 5 levels "BOV" > >"CAP" > >"CER" "OVI" "POR" > >Variable "type" has 11 levels "BRD" "CL" ... "OTHER" > > > >I would like to replace the values on species by the values on types > >only if > >species is == "POR" > >I tried: &
Re: [R] ifelse() question
Francisco, a more reproducible example would have helped but see if the following helps in your understanding # Create dataset df <- data.frame( type=1:5, species=LETTERS[1:5] ) df type species 11 A 22 B 33 C 44 D 55 E df[ ,2] == "E" [1] FALSE FALSE FALSE FALSE TRUE # Note that you need to coerce as.character inside ifelse attach(df) (df[ ,2] <- ifelse( species == "E", type, as.character(species) )) [1] "A" "B" "C" "D" "5" detach(df) df type species 11 A 22 B 33 C 44 D 55 5 On Fri, 2004-10-29 at 18:03, F Z wrote: > Thanks for you reply Peter. I tried using as.character and then converting > to factors but it did not work since it generates missing values for all the > dat[,4]=="POR". See: > > >dat[1:5,4] > [1] BOV POR BOV POR BOV > Levels: BOV CAP CER OVI POR > > test<-dat > > >test[,4]<-as.character(test[,4]) > >test[,5]<-as.character(test[,5]) > >test[test[,4]=="POR",4]<-test[test[,4]=="POR",5] > Error in "[<-.data.frame"(`*tmp*`, test[, 4] == "POR", 4, value = c(NA, : > missing values are not allowed in subscripted assignments of data > frames > >test[,4]<-as.factor(test[,4]) > >test[,5]<-as.factor(test[,5]) > > >test[1:5,4] > [1] BOV BOV BOV > Levels: BOV CAP CER OVI > > > Any suggestions? > > Thanks again! > > Francisco > > > >From: "Peter Alspach" <[EMAIL PROTECTED]> > >To: <[EMAIL PROTECTED]>,<[EMAIL PROTECTED]> > >Subject: Re: [R] ifelse() question > >Date: Fri, 29 Oct 2004 13:33:54 +1300 > > > > > >Francisco > > > >Did you try changing the factors to character, with as.character? > > > >Also you don't really need ifelse() for this. Something like the > >following (untested) should do it: > > > >dat[,4] <- as.character(dat[,4]) > >dat[,5] <- as.character(dat[,5]) > >dat[dat[,4]=='POR',4] <- dat[dat[,4]=='POR',5] > >dat[,4] <- as.factor(dat[,4]) > >dat[,5] <- as.factor(dat[,5]) > > > > > >Peter Alspach > > > > >>> "F Z" <[EMAIL PROTECTED]> 29/10/04 12:48:54 >>> > >Hi > > > >I have a data.frame with dim = 18638 (rows) 6 (cols) > > > >names(dat) > >[1] "id" "long""lat" "species" "type""size" > > > >Variable "species" and "type" are factors. Species has 5 levels "BOV" > >"CAP" > >"CER" "OVI" "POR" > >Variable "type" has 11 levels "BRD" "CL" ... "OTHER" > > > >I would like to replace the values on species by the values on types > >only if > >species is == "POR" > >I tried: > > > >x<-ifelse(dat$species %in% "POR",dat$type,dat$species) > >dat[,4]<-x > >but levels(x) > >[1] "1" "2" "3" "4" "5" "6" "8" "9" "10" "11" "12" > > > >So x changes the factor names by numbers. I can not use factor() to > >recover > >the names since the resulting factors in x are a mixture of factors > >from > >species and type. > > > >I also tried > > > >x<-gsub(pattern = "POR",replacement= factor(dat$type),dat$species) > >with > >same behavior. > > > >Apparently I did not have my granola bar today so I can't find a > >solution! > >Any help is greatly appreciated > > > >Thanks! > > > >Francisco > > > >__ > >[EMAIL PROTECTED] mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide! > >http://www.R-project.org/posting-guide.html > > > >__ > > > >The contents of this e-mail are privileged and/or confidential to the > >named recipient and are not to be used by any other person and/or > >organisation. If you have received this e-mail in error, please notify > >the sender and delete all material pertaining to this e-mail. > >__ > > __ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] ifelse() question
Thanks for you reply Peter. I tried using as.character and then converting to factors but it did not work since it generates missing values for all the dat[,4]=="POR". See: dat[1:5,4] [1] BOV POR BOV POR BOV Levels: BOV CAP CER OVI POR test<-dat test[,4]<-as.character(test[,4]) test[,5]<-as.character(test[,5]) test[test[,4]=="POR",4]<-test[test[,4]=="POR",5] Error in "[<-.data.frame"(`*tmp*`, test[, 4] == "POR", 4, value = c(NA, : missing values are not allowed in subscripted assignments of data frames test[,4]<-as.factor(test[,4]) test[,5]<-as.factor(test[,5]) test[1:5,4] [1] BOV BOV BOV Levels: BOV CAP CER OVI Any suggestions? Thanks again! Francisco From: "Peter Alspach" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]>,<[EMAIL PROTECTED]> Subject: Re: [R] ifelse() question Date: Fri, 29 Oct 2004 13:33:54 +1300 Francisco Did you try changing the factors to character, with as.character? Also you don't really need ifelse() for this. Something like the following (untested) should do it: dat[,4] <- as.character(dat[,4]) dat[,5] <- as.character(dat[,5]) dat[dat[,4]=='POR',4] <- dat[dat[,4]=='POR',5] dat[,4] <- as.factor(dat[,4]) dat[,5] <- as.factor(dat[,5]) Peter Alspach >>> "F Z" <[EMAIL PROTECTED]> 29/10/04 12:48:54 >>> Hi I have a data.frame with dim = 18638 (rows) 6 (cols) names(dat) [1] "id" "long""lat" "species" "type""size" Variable "species" and "type" are factors. Species has 5 levels "BOV" "CAP" "CER" "OVI" "POR" Variable "type" has 11 levels "BRD" "CL" ... "OTHER" I would like to replace the values on species by the values on types only if species is == "POR" I tried: x<-ifelse(dat$species %in% "POR",dat$type,dat$species) dat[,4]<-x but levels(x) [1] "1" "2" "3" "4" "5" "6" "8" "9" "10" "11" "12" So x changes the factor names by numbers. I can not use factor() to recover the names since the resulting factors in x are a mixture of factors from species and type. I also tried x<-gsub(pattern = "POR",replacement= factor(dat$type),dat$species) with same behavior. Apparently I did not have my granola bar today so I can't find a solution! Any help is greatly appreciated Thanks! Francisco __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. __ __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] ifelse() question
Francisco Did you try changing the factors to character, with as.character? Also you don't really need ifelse() for this. Something like the following (untested) should do it: dat[,4] <- as.character(dat[,4]) dat[,5] <- as.character(dat[,5]) dat[dat[,4]=='POR',4] <- dat[dat[,4]=='POR',5] dat[,4] <- as.factor(dat[,4]) dat[,5] <- as.factor(dat[,5]) Peter Alspach >>> "F Z" <[EMAIL PROTECTED]> 29/10/04 12:48:54 >>> Hi I have a data.frame with dim = 18638 (rows) 6 (cols) names(dat) [1] "id" "long""lat" "species" "type""size" Variable "species" and "type" are factors. Species has 5 levels "BOV" "CAP" "CER" "OVI" "POR" Variable "type" has 11 levels "BRD" "CL" ... "OTHER" I would like to replace the values on species by the values on types only if species is == "POR" I tried: x<-ifelse(dat$species %in% "POR",dat$type,dat$species) dat[,4]<-x but levels(x) [1] "1" "2" "3" "4" "5" "6" "8" "9" "10" "11" "12" So x changes the factor names by numbers. I can not use factor() to recover the names since the resulting factors in x are a mixture of factors from species and type. I also tried x<-gsub(pattern = "POR",replacement= factor(dat$type),dat$species) with same behavior. Apparently I did not have my granola bar today so I can't find a solution! Any help is greatly appreciated Thanks! Francisco __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ The contents of this e-mail are privileged and/or confidenti...{{dropped}} __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] ifelse() question
Hi I have a data.frame with dim = 18638 (rows) 6 (cols) names(dat) [1] "id" "long""lat" "species" "type""size" Variable "species" and "type" are factors. Species has 5 levels "BOV" "CAP" "CER" "OVI" "POR" Variable "type" has 11 levels "BRD" "CL" ... "OTHER" I would like to replace the values on species by the values on types only if species is == "POR" I tried: x<-ifelse(dat$species %in% "POR",dat$type,dat$species) dat[,4]<-x but levels(x) [1] "1" "2" "3" "4" "5" "6" "8" "9" "10" "11" "12" So x changes the factor names by numbers. I can not use factor() to recover the names since the resulting factors in x are a mixture of factors from species and type. I also tried x<-gsub(pattern = "POR",replacement= factor(dat$type),dat$species) with same behavior. Apparently I did not have my granola bar today so I can't find a solution! Any help is greatly appreciated Thanks! Francisco __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html