Explicitly convert it to a factor... data$SOCIAL_STATUS<-factor(ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C", data$SOCIAL_STATUS))
However, note that this would, in general, change the levels attribute to the levels actually present in the converted vector. If you wish to (and it makes sense to) retain the original levels, you will have to set them explicitly in the factor() call, I believe. I believe the reason this occurs is that ifelse() could (and usually would) change the values present, which would mess up the levels of the factor. So default conversion to character seems like desirable behavior to me. It's then up to the user to recreate the factor as appropriate. Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mark Na Sent: Wednesday, June 24, 2009 9:34 AM To: r-help@r-project.org Subject: [R] How to avoid ifelse statement converting factor to character Hi R-helpers, Please see the below R output. The problem is that after running the ifelse statement, data$SOCIAL_STATUS is converted from a factor to a character. Is there some way I can avoid this conversion? Thanks in advance, Mark Na > str(data) 'data.frame': 2100 obs. of 11 variables: $ DATE : Factor w/ 5 levels "4-Jun-09","7-May-09",..: 1 1 1 1 1 1 1 1 1 1 ... $ POND_ID : Factor w/ 113 levels "10","18","19",..: 8 8 8 8 8 8 8 8 8 8 ... $ STATUS : num 1 1 1 1 1 1 1 1 1 1 ... $ SPECIES : Factor w/ 25 levels "AGWT","AMCO",..: 10 10 7 7 3 5 5 5 5 2 ... $ SOCIAL_STATUS : Factor w/ 8 levels "A","B","D","E",..: 4 1 4 1 4 4 4 4 1 6 ... $ COUNT_OF_GROUPS: num 1 1 1 1 1 3 3 3 1 2 ... $ MALE : num 1 1 1 1 1 1 1 1 1 0 ... $ FEMALE : num 1 0 1 0 1 1 1 1 0 0 ... $ NOSEX : num 0 0 0 0 0 0 0 0 0 2 ... $ UPLAND : num 0 0 0 0 0 0 0 0 0 0 ... $ TAG : num 0 0 0 0 0 0 0 0 0 0 ... > data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C", data$SOCIAL_STATUS) > str(data) 'data.frame': 2100 obs. of 11 variables: $ DATE : Factor w/ 5 levels "4-Jun-09","7-May-09",..: 1 1 1 1 1 1 1 1 1 1 ... $ POND_ID : Factor w/ 113 levels "10","18","19",..: 8 8 8 8 8 8 8 8 8 8 ... $ STATUS : num 1 1 1 1 1 1 1 1 1 1 ... $ SPECIES : Factor w/ 25 levels "AGWT","AMCO",..: 10 10 7 7 3 5 5 5 5 2 ... $ SOCIAL_STATUS : chr "4" "1" "4" "1" ... $ COUNT_OF_GROUPS: num 1 1 1 1 1 3 3 3 1 2 ... $ MALE : num 1 1 1 1 1 1 1 1 1 0 ... $ FEMALE : num 1 0 1 0 1 1 1 1 0 0 ... $ NOSEX : num 0 0 0 0 0 0 0 0 0 2 ... $ UPLAND : num 0 0 0 0 0 0 0 0 0 0 ... $ TAG : num 0 0 0 0 0 0 0 0 0 0 ... [[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.