Hello, I have a column with a bunch of letters. I would like to keep some of these letters (A,C,D,L) and turn the rest into 'X'. I have tried using ifelse with '|' in between the argument but it didn't work nor did 4 separate ifelse statements. Example, I currently have: Letters??? A??? B??? C??? D??? E I would like to have: Letters??? A??? X??? C??? D??? X Thank you [[alternative HTML version deleted]]
Nia Gupta <nia_gupta <at> yahoo.com> writes:> > Hello,> I have a column with a bunch of letters. I would like to keep some > of these letters (A,C,D,L) and turn the rest into 'X'.> I have tried using ifelse with '|' in between the argument but it > didn't work nor did 4 separate ifelse statements.> Example, I currently have: > Letters??? A??? B??? C??? D??? E > I would like to have: > Letters??? A??? X??? C??? D??? X > Thank you%in% will be helpful Letters <- LETTERS[1:5] targets <- c("A","C","D","L") Letters[!Letters %in% targets] <- "X"
On 10/4/2014 8:21 AM, Nia Gupta wrote:> Hello, > > I have a column with a bunch of letters. I would like to keep some of these letters (A,C,D,L) and turn the rest into 'X'. > > I have tried using ifelse with '|' in between the argument but it didn't work nor did 4 separate ifelse statements. > > Example, I currently have: > Letters A B C D E > I would like to have: > Letters A X C D X > Thank you > > [[alternative HTML version deleted]]try: let = sample(LETTERS[1:5],100,replace=TRUE) let let1 = ifelse(let %in% c('A','C','D'),let,'X') let1> ______________________________________________ > 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.