Value "less than" is removed in R

1

I have a df that contains negative times, for example:

-1: -14: -56

I would like to take all the times that were less than 00:00:00 and put 00:00:00 instead.

I'm currently doing a lot of work on replacing these objects:

df041[,1:61][df041[,1:61]=="-1:-38:-1"] <- "00:00:00"
df041[,1:61][df041[,1:61]=="-1:00:-33"] <- "00:00:00"
df041[,1:61][df041[,1:61]=="-8:-57:-54"] <- "00:00:00"

Does anyone have any idea how to do this?

    
asked by anonymous 24.10.2018 / 15:25

1 answer

5

Here's a solution with pacte lubridate . It begins by defining a function, negativo , which only modifies the values with the format "HH:MM:SS" where at least one of these numbers is negative. Then apply this function to the columns of the dataframe.

library(lubridate)

negativo <- function(x, replace = "00:00:00"){
  y <- hms(x)
  h <- hour(y) < 0
  m <- minute(y) < 0
  s <- second(y) < 0
  x[h | m | s] <- replace
  x
}

negativo(c("-1:-38:-1", "-1:00:-33", "-8:-57:-54"))
#[1] "00:00:00" "00:00:00" "00:00:00"


df041[, 1:61] <- lapply(df041[, 1:61], negativo)

The function is tested, sapply is not since there is no test data but I do not think there are any problems.

    
24.10.2018 / 17:05