Reputation: 165
Data
a = c('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
I am trying to replace days of weeks as weekdays/weekend using below code,
a = ifelse(a==c('Monday','Tuesday','Wednesday','Thursday','Friday'),'Weekday',
'Weekend')
However I get the below warning message with expected results
Warning message:
In a == c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") :
longer object length is not a multiple of shorter object length
is my approach correct ?is there a better approach ?
when I do this on a large data frame over 40,000 rows I don't get the expected result.
Upvotes: 0
Views: 459
Reputation: 33498
Your approach is almost correct.
You just need (as docendo discimus pointed out) %in%
in place of ==
.
ifelse(a %in% c('Monday','Tuesday','Wednesday','Thursday','Friday'), 'Weekday', 'Weekend')
And then you could slightly simplify this with:
ifelse(a %in% c('Sunday', 'Saturday'), 'Weekend', 'Weekday')
Upvotes: 1