On Dec 3, 2013, at 5:37 AM, Hadley Wickham wrote: > A better solution to this problem is to use character indexing: > > x <- c("Tuesday", "Thursday", "Sunday") > c(Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, > Saturday = 6, Sunday = 7)[x] > > http://adv-r.had.co.nz/Subsetting.html#lookup-tables-character-subsetting
That does seem more expressive than using match: x <- c("Tuesday", "Thursday", "Sunday") match(x, c('Monday', 'Tuesday', 'Wednesday' ,'Thursday' , 'Friday', 'Saturday', 'Sunday') ) [1] 2 4 7 It would also lend itself well to grouping operations x <- c("Tuesday", "Thursday", "Sunday") c(Monday = 'first half', Tuesday = 'first half', Wednesday = "hump", Thursday = 'second half', Friday = 'second half', Saturday = "weekend", Sunday = "weekend")[x] Tuesday Thursday Sunday "first half" "second half" "weekend" The corresponding use case with match would be: c(rep('first half',2),"hump", rep('second half',2), rep("weekend",2) )[ match(x, c('Monday', 'Tuesday', 'Wednesday' ,'Thursday' , 'Friday', 'Saturday', 'Sunday') ) ] [1] "first half" "second half" "weekend" And your recommended method has the virtue of performing the same partial matching allowed by charmatch: x2 <- substr(x, 1,3) charmatch(x2, c('Monday', 'Tuesday', 'Wednesday' ,'Thursday' , 'Friday', 'Saturday', 'Sunday') ) #[1] 2 4 7 c(Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7)[x] # Tuesday Thursday Sunday # 2 4 7 -- David. > > Hadley > > On Mon, Dec 2, 2013 at 6:33 PM, Bill <william...@gmail.com> wrote: >> ifelse ((day_of_week == "Monday"),1, >> ifelse ((day_of_week == "Tuesday"),2, >> ifelse ((day_of_week == "Wednesday"),3, >> ifelse ((day_of_week == "Thursday"),4, >> ifelse ((day_of_week == "Friday"),5, >> ifelse ((day_of_week == "Saturday"),6,7))))))) >> >> >> In code like the above, day_of_week is a vector and so day_of_week == >> "Monday" will result in a boolean vector. Suppose day_of_week is Monday, >> Thursday, Friday, Tuesday. So day_of_week == "Monday" will be >> True,False,False,False. I think that ifelse will test the first element and >> it will generate a 1. At this point it will not have run day_of_week == >> "Tuesday" yet. Then it will test the second element of day_of_week and it >> will be false and this will cause it to evaluate day_of_week == "Tuesday". >> My question would be, does the evaluation of day_of_week == "Tuesday" >> result in the generation of an entire boolean vector (which would be in >> this case False,False,False,True) or does the ifelse "manage the indexing" >> so that it only tests the second element of the original vector (which is >> Thursday) and for that matter does it therefore not even bother to generate >> the first boolean vector I mentioned above (True,False,False,False) but >> rather just checks the first element? >> Not sure if I have explained this well but if you understand I would >> appreciate a reply. >> Thanks. >> >> [[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. > > > > -- > http://had.co.nz/ > > ______________________________________________ > 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. David Winsemius Alameda, CA, USA ______________________________________________ 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.