1
votes

I got this dataframe:

       bedtime           waketime
 2021-01-01 21:55:00  2021-01-02 09:00:00
 2021-01-02 00:15:00  2021-01-03 08:45:00
 2021-01-03 01:05:00  2021-01-04 08:40:00
 2021-01-04 02:01:00  2021-01-05 10:50:00
 2021-01-05 00:20:00  2021-01-06 08:45:00
 2021-01-06 02:25:00  2021-01-07 10:55:00
 2021-01-07 00:25:00  2021-01-08 06:45:00
 2021-01-08 00:10:00  2021-01-09 09:15:00
 2021-01-09 00:20:00  2021-01-10 08:15:00
 2021-01-10 00:00:00  2021-01-11 08:45:00

I want to change the date for the times after midnight (+ 1 day).

I did this with a different dataframe before with this code and it worked perfectly

df[hour(df$bedtime) < 12, "bedtime"] <- df$bedtime[hour(df$bedtime) < 12, "bedtime"] + (24*60*60)

But now I got the error "Incorrect number of dimensions".

I dont't know whats wrong with this dataframe, the variables have the same type than the others.

1
Try : df[hour(df$bedtime) < 12, "bedtime"] <- df[hour(df$bedtime) < 12, "bedtime"] + (24*60*60)Ronak Shah
Yes thats what I already tried, but the error occurspsycho95
How do you build the dataframe? Have you got any code which can be used to reproduce the df shown in your example?BillyBouw
Oh yes, my bad! Thank your very much, this worked! The only problem is that it doesn't work, if the dataframe contains NAs. If I can't remove those cases because they contain other important information, how can I handle this?psycho95

1 Answers

1
votes

Try this one using which, which frees you from the NA issue.

d[] <- lapply(d, as.POSIXct)  ## first transform to POSIX if isn't yet
d <- transform(d, bedtime.old=bedtime, waketime.old=waketime)  ## copy for demonstration

ix <- which(strftime(d$bedtime, "%H") < 12)
d[ix, "bedtime"] <- d[ix, "bedtime"] + 24*60*60
d
#                bedtime            waketime         bedtime.old        waketime.old
# 1  2021-01-01 21:55:00 2021-01-02 09:00:00 2021-01-01 21:55:00 2021-01-02 09:00:00
# 2  2021-01-03 00:15:00 2021-01-03 08:45:00 2021-01-02 00:15:00 2021-01-03 08:45:00
# 3  2021-01-04 01:05:00 2021-01-04 08:40:00 2021-01-03 01:05:00 2021-01-04 08:40:00
# 4  2021-01-05 02:01:00 2021-01-05 10:50:00 2021-01-04 02:01:00 2021-01-05 10:50:00
# 5  2021-01-06 00:20:00 2021-01-06 08:45:00 2021-01-05 00:20:00 2021-01-06 08:45:00
# 6  2021-01-07 02:25:00                <NA> 2021-01-06 02:25:00                <NA>
# 7  2021-01-08 00:25:00 2021-01-08 06:45:00 2021-01-07 00:25:00 2021-01-08 06:45:00
# 8  2021-01-09 00:10:00 2021-01-09 09:15:00 2021-01-08 00:10:00 2021-01-09 09:15:00
# 9                 <NA> 2021-01-10 08:15:00                <NA> 2021-01-10 08:15:00

Data:

d <- structure(list(bedtime = c("2021-01-01 21:55:00", "2021-01-02 00:15:00", 
"2021-01-03 01:05:00", "2021-01-04 02:01:00", "2021-01-05 00:20:00", 
"2021-01-06 02:25:00", "2021-01-07 00:25:00", "2021-01-08 00:10:00", 
NA, "2021-01-10 00:00:00"), waketime = c("2021-01-02 09:00:00", 
"2021-01-03 08:45:00", "2021-01-04 08:40:00", "2021-01-05 10:50:00", 
"2021-01-06 08:45:00", NA, "2021-01-08 06:45:00", "2021-01-09 09:15:00", 
"2021-01-10 08:15:00", "2021-01-11 08:45:00")), class = "data.frame", row.names = c(NA, 
-10L))