-2

I want to add a column of days each week to my dataset which contains continuous days and continuous weeks already for a large dataset

I've tried to use the floor function and the rep function already

Edit I'm attempting to automate the final output mydata$day_of_week

time_in_hrs<-c(0,2,12,24,25,29,30,32,38,43,57,82,93,105,199,205,245,263)
mydata<-as.data.frame(time_in_hrs)
mydata$time_in_days<-floor(mydata$time_in_hrs/24)+1
mydata$time_in_weeks<-floor(mydata$time_in_days/8)+1
mydata$day_of_week<-c(1,1,1,2,2,2,2,2,2,2,3,4,4,5,2,2,4,4)
Sparky
  • 349
  • 1
  • 12

2 Answers2

1

Assuming you're trying to automate the column mydata$day_of_week that you're now doing manually, you can do this as follows:

mydata$day_of_week_auto <- mydata$time_in_days - (mydata$time_in_weeks - 1) * 7
Sven
  • 1,203
  • 1
  • 5
  • 14
0

with the rep-command you can try the following:

mydata$days <- rep(c("Monday", "Tuesday", "Wednesday", Thursday", 
"Friday", "Saturday", "Sunday"), times = 5)

mydata$days <- rep(1:7, times = 5)

Hope this might help you.

Best, Veronika

  • Hi @veronika unfortunately, this doesn't work as it doesn't recognise the variation in the time/day columns – Sparky May 27 '19 at 15:23