I'm a new R user. I've been trying to compute the time difference between two time points, and calculate a percentage value (habitual_efficiency). Using this percentage value, I need to assign specific value to a new variable (i).
time_difference1 <- as.numeric(difftime(strptime(var$psqi_1_bedtime, "%H:%M"), strptime(var$psqi_3_waketime, "%H:%M")))
timedifference <- abs (24 - time_difference1)
var4 <- data.frame (habitual_efficiency = 1: nrow(var))
transmute (var, habitual_efficiency = (var$psqi_4_sleeptime*100/timedifference))
var4 <- data.frame (comp4score = 1: nrow(var))
var4 <- var4 %>% mutate(comp4score = case_when(var$habitual_efficiency > 84.00 ~ 0, var$habitual_efficiency > 74.00 & var$habitual_efficiency <= 84.00 ~ 1, var$habitual_efficiency > 65.00 & var$habitual_efficiency <= 74.00 ~ 2, var$habitual_efficiency <= 65.00 ~ 3, TRUE ~ NaN
)
)
var4
When I run this code, I keep getting the following error: Error in mutate_impl(.data, dots) :
Column habitual_efficiency
must be length 430 (the number of rows) or one, not 0
Error in mutate_impl(.data, dots) :
Column i
must be length 430 (the number of rows) or one, not 0
How would I approach solving this?