5

I have a dataframe

       Date      repair     
 <date>           <dbl>        
 2018-07-01        4420    
 2018-07-02          NA   
 2018-07-03          NA
 2018-07-04          NA
 2018-07-05          NA

Where 4420 is time in minutes. I'm trying to get this:

       Date      repair     
 <date>           <dbl>        
 2018-07-01        1440    
 2018-07-02        1440   
 2018-07-03        1440
 2018-07-04         100
 2018-07-05          NA

Where 1440 - minutes in one day and 100 what is left. I made it with loop. Can this be achieved in a more elegant way?

Dmytro Fedoriuk
  • 331
  • 3
  • 11
  • There could be many edge cases for this question but for starters can you clarify 1) What would be your output when `repair = c(4420, NA, NA, 4420, NA)` ? 2) Output for `repair = c(4420, 100, NA, 4420, NA)`. Is the above two inputs possible or they will never occur? – Ronak Shah Feb 06 '19 at 14:18
  • They will never occur. Overlapping is impossible. – Dmytro Fedoriuk Feb 06 '19 at 14:23
  • I'm sorry. This scenario ```repair = c(4420, 100, NA, 4420, NA)``` is possible. – Dmytro Fedoriuk Feb 06 '19 at 14:44
  • 1
    @DmytroFedoriuk I might be best to ask a new question then. – markus Feb 06 '19 at 15:05

3 Answers3

2

You could write a little function for that task

f <- function(x, y, length_out) {
  remainder <- x %% y 
  if(remainder == 0) {
    `length<-`(rep(y, x %/% y), length_out)
  } else {
    `length<-`(c(rep(y, x %/% y), remainder), length_out)
  }
}

Input

x <- 4420
y <- 24 * 60

Result

f(x, y, length_out = 10)
# [1] 1440 1440 1440  100   NA   NA   NA   NA   NA   NA

length_out should probably be equal to nrow(your_data)

markus
  • 25,843
  • 5
  • 39
  • 58
2

With dplyr:

library(dplyr)

df %>%
  mutate(
    repair = c(rep(1440, floor(repair[1] / 1440)), 
               repair[1] %% 1440, 
               rep(NA, n() - length(c(rep(1440, floor(repair[1] / 1440)), repair[1] %% 1440))))
  )

Output:

        Date repair
1 2018-07-01   1440
2 2018-07-02   1440
3 2018-07-03   1440
4 2018-07-04    100
5 2018-07-05     NA
arg0naut91
  • 14,574
  • 2
  • 17
  • 38
  • 1
    It's a great solution! But if the first number of df is the multiple of 1440, such as 4320, your output will be `1440 1440 1440 0 NA`, not `1440 1440 1440 NA NA`. – Darren Tsai Feb 06 '19 at 15:23
  • 1
    Thanks, you're completely right; however I'm not sure about what OP wants in this case, and from what I can see there are some additional requirements to the question (for which I feel additional elaboration is needed). – arg0naut91 Feb 06 '19 at 15:36
2

A recursive solution:

fun <- function(x, y, i = 0){
  if(x <= y) c(rep(y, i), x) else fun(x-y, y, i+1)
}

fun(4420, 1440)[1:nrow(df)]
# [1] 1440 1440 1440  100   NA
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51