0

I have the below dataframe:

For Country = Hong Kong or Macau, I need to add 6 hours.

Input

number indate country
1 2020-05-05 7:41:28.277 Hong Kong
2 2020-05-05 17:41:28.277 China
3 2020-05-05 19:41:28.277 Macau
4 2020-05-05 17:41:28.277 China
5 2020-05-05 17:41:28.277 USA
6 2020-05-05 22:41:28.277 Hong Kong

Desired output:

number indate country indate1
1 2020-05-05 7:41:28.277 Hong Kong 2020-05-05 13:41:28.277
2 2020-05-15 17:41:28.277 China 2020-05-15 17:41:28.277
3 2020-05-12 19:41:28.277 Macau 2020-05-13 01:41:28.277
4 2020-05-25 17:41:28.277 China 2020-05-25 17:41:28.277
5 2020-05-05 17:41:28.277 USA 2020-05-05 17:41:28.277
6 2020-05-13 22:41:28.277 Hong Kong 2020-05-14 04:41:28.277

Below is the code I have written:

library(lubridate)

hour_addition = function(dataf) {
        country = dataf[3]
        date= dataf[2]
        if (country == 'Hong Kong' | country = 'Macau'){
        date=as_datetime(date)
        date1 = date+dhours(6)
        date1 = as.character(date1)
        } else {
                date1 = date
        }
            return (date1)
        }
        
    new_date = apply(df,1,hour_addition)    
    df$indate1=new_date

I am not getting the desired results, Thanks in Advance. :)

vik
  • 43
  • 6
  • Please provide your data in a reproducible format: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ben G Aug 25 '21 at 14:31
  • Don't use apply. It will mangle your dataframe. instead just work with the indate column. `Input$Indate1 <- Input$indate+dhours(6)` – IRTFM Aug 25 '21 at 20:21
  • @IRTFM the condition is if the country is equal to Hong Kong and Macau then only add 6 hour, rest all cases use the existing indate. – vik Aug 26 '21 at 03:53

1 Answers1

0

Do you need to do it with UDF? It is much easier with dplyr package:

library(dplyr)
library(lubridate)

df <- data.frame(
  number = c(1,2,3,4,5,6)
  ,indate = c('2020-05-05 17:41','2020-05-05 17:41','2020-05-05 17:41','2020-05-05 17:41','2020-05-05 17:41','2020-05-05 17:41')
  ,country = c("Hong Kong","China","Macau","China","USA","Hong Kong")
)

df <- df %>% 
  mutate(indate = ymd_hm(indate)
                ,indate1 = if_else(country %in% c('Hong Kong','Macau'), indate + hours(6), indate) )
Arthur G.
  • 93
  • 1
  • 10
  • the condition is if the country is equal to Hong Kong and Macau then only add 6 hour, rest all cases use the existing indate. – vik Aug 26 '21 at 03:52
  • "If_else" is the function you are looking for. I've just updated my solution. – Arthur G. Aug 26 '21 at 07:55
  • G, I have asked a continuation question can u please look into it. – vik Aug 26 '21 at 14:33