Using Branching Statements on entire list rather than individual items in R -
this follow-up question, though question here independent of one, using apply functions instead of , branching statements in r
i have data frame:
date close weekday dayofmonth 290 1991-02-22 365.65 friday 22 295 1991-03-01 370.47 friday 1 300 1991-03-08 374.95 friday 8 305 1991-03-15 373.59 friday 15 310 1991-03-22 367.48 friday 22 314 1991-03-28 375.22 thursday 28 319 1991-04-05 375.36 friday 5 324 1991-04-12 380.40 friday 12 329 1991-04-19 384.20 friday 19 334 1991-04-26 379.02 friday 26 339 1991-05-03 380.80 friday 3
i want create column called weekofcycle figures out week of month given date in based on day of month. using function based on aforementioned question.
as.integer(cut(data$dayofmonth, c(-inf, 7, 14, 21, 28, inf)))
the above line fridays, thursdays should
as.integer(cut(data$dayofmonth, c(-inf, 6, 13, 20, 27, inf)))
desired output:
date close weekday dayofmonth weekofcycle 290 1991-02-22 365.65 friday 22 4 295 1991-03-01 370.47 friday 1 1 300 1991-03-08 374.95 friday 8 2 305 1991-03-15 373.59 friday 15 3 310 1991-03-22 367.48 friday 22 4 314 1991-03-28 375.22 thursday 28 5 319 1991-04-05 375.36 friday 5 1 324 1991-04-12 380.40 friday 12 2 329 1991-04-19 384.20 friday 19 3 334 1991-04-26 379.02 friday 26 4 339 1991-05-03 380.80 friday 3 1
i have tried work lapply function though doesn't work:
data$weekofcycle <- with(data, lapply(weekday, function(i){ if (i == "friday"){ as.integer(cut(dayofmonth, c(-inf, 7, 14, 21, 28, inf))) } else if (i == "thursday"){ as.integer(cut(dayofmonth, c(-inf, 6, 13, 20, 27, inf))) } } ))
i feel close, missing something. ideally, i'd without if , else statement don't know how.
reproducible code data frame:
structure(list(date = structure(c(7722, 7729, 7736, 7743, 7750, 7756, 7764, 7771, 7778, 7785, 7792), class = "date"), close = c(365.65, 370.47, 374.95, 373.59, 367.48, 375.22, 375.36, 380.4, 384.2, 379.02, 380.8), weekday = c("friday", "friday", "friday", "friday", "friday", "thursday", "friday", "friday", "friday", "friday", "friday"), dayofmonth = c(22, 1, 8, 15, 22, 28, 5, 12, 19, 26, 3)), .names = c("date", "close", "weekday", "dayofmonth"), row.names = c(290l, 295l, 300l, 305l, 310l, 314l, 319l, 324l, 329l, 334l, 339l), class = "data.frame")
thank help.
it if included desired output sample data can check whether i'm getting results desire, how method
#define shift each week day offset<-c(friday=0, thursday=1) #see how may times 7 date of month monthweek <- unname((data$dayofmonth+offset[data$weekday]-1) %/% 7 +1) cbind(data, monthweek) # date close weekday dayofmonth monthweek # 290 1991-02-22 365.65 friday 22 4 # 295 1991-03-01 370.47 friday 1 1 # 300 1991-03-08 374.95 friday 8 2 # 305 1991-03-15 373.59 friday 15 3 # 310 1991-03-22 367.48 friday 22 4 # 314 1991-03-28 375.22 thursday 28 5 # 319 1991-04-05 375.36 friday 5 1 # 324 1991-04-12 380.40 friday 12 2 # 329 1991-04-19 384.20 friday 19 3 # 334 1991-04-26 379.02 friday 26 4 # 339 1991-05-03 380.80 friday 3 1
in particular cases, there no need complicated if
statements. can done pretty simple math.
Comments
Post a Comment