r - Record variable to a dummy variable using weekdays -
i have variable starting monday lists each date 1-7. want change weekday vs. weekend, 0-1 respectively create dummy variable. know how one, can't figure out how include 6 , 7 in iterations of code.
for example, put following:
flights$dayweek <-factor(ifelse(as.numeric(flights$dayweek)==6, 1,0))
my intent above code find anywhere says 6 & 7, replace 1 , else 0 variable dayweek in flights data set. problem above 6 , not 7. don't know how include 7 in data set. have tried:
flights$dayweek <-factor(ifelse(as.numeric(flights$dayweek)==6:7, 1,0)) flights$dayweek <-factor(ifelse(as.numeric(flights$dayweek)==c(6,7), 1,0))
and have looked @ other common dummy variables topics, seem simple 1 0 male/female , know how that. greater 5 function? sample data below:
schedtime carrier deptime dest distance date dayweek daymonth delay 1700 ru 1651 wer 213 1401 4 1 ontime 1800 ru 1402 ewr 199 1401 6 1 delayed
use %in%
operator test inclusion in vector.
# using example dataset flights <- data.frame(dayweek = rep(1:7, 2), "flight" = letters[1:14]) flights$dayweek <-factor(ifelse(as.numeric(flights$dayweek) %in% c(6, 7), 1,0)) > flights dayweek flight 1 0 2 0 b 3 0 c 4 0 d 5 0 e 6 1 f 7 1 g 8 0 h 9 0 10 0 j 11 0 k 12 0 l 13 1 m 14 1 n
Comments
Post a Comment