I am having trouble writing a function to replace a for() loop in R.
My data looks like this:
dat1<-rep(sprintf("%02d", 0:52),10)
dat1<-as.data.frame(dat1[40:length(dat1)])
colnames(dat1)<-c("wk")
wk.seas<-c(rep("win",9),rep("spr",13),rep("sum",13),rep("aut",13),rep("win",6))
wks<-sprintf("%02d", 0:53)
This loop produces what I want:
for(j in seq_along(dat1[,1])){
dat1$season[dat1[,1]==wks[j]]<-wk.seas[j]
}#works
I am looking to create a vector in the df with a value for season that corresponds to the week of the year.
Here is my attempt to replicate this loop with a function/lapply combo:
lapply(seq_along(dat1$wk), function(d){
dat1$season[dat1$wk==wks[d]]<-wk.seas[d]
}) #not working
Thank you (edited to add missing code)