简体   繁体   中英

Subtracting seconds from one row and setting result to a different row in R

I have a dataframe where the first column is Time. My current output looks like:

0:0:0
0:0:0
5:43:42 pm
5:43:52 pm

I'm trying to make a portion of my code search for any rows that have the 0:0:0 value in them and subtract 10 seconds from the closest row with a value in it. The desired outcome would look like:

5:43:22 pm
5:43:32 pm
5:43:42 pm
5:43:52 pm

I've written this portion of the code to do this for me. It runs with no errors, but doesn't actually change any values.

  entries<-length(data$Time)
  for(j in entries:1)
    if(data[j,1]=="0:0:0"){
      for(k in j-1:1)
        if(data[k,1]!="0:0:0"){
          data[k,1] <- as.POSIXct(data[k,1], format = "%H:%M:%S")
          value <- format(data[k,1] + seconds(10))
          data[j,1] <- value
          break
        }
    }

Any suggestions to go about creating this?

Assuming that the last value in your data would not be '0:0:0' you can do the following:

Replace Time with NA where the value is 0:0:0 and turn them to POSIXct class.

library(dplyr)

df %>%
  mutate(Time = na_if(Time, '0:0:0'), 
         Time1 = as.POSIXct(Time, format = '%T')) -> df1

Replace NA value by subtracting 10 second from the next value.

for(i in nrow(df1):1) {
  if(is.na(df1$Time1[i])) {
    df1$Time1[i] <- df1$Time1[i + 1] - 10
  }
}

Change POSIXct in the desired format (H:M:S) that we need.

df1 %>%
  mutate(Time = format(Time1, '%T'), 
         Time1 = NULL) -> result

result

#      Time
#1 05:43:22
#2 05:43:32
#3 05:43:42
#4 05:43:52

data

df <- structure(list(Time = c("0:0:0", "0:0:0", "5:43:42", "5:43:52"
)), class = "data.frame", row.names = c(NA, -4L))

Here is a data.table approach, assuming you mean by "closest row with a value" the nearest following row that is not "0:0:0":

library(data.table)
DT <- data.table(Time=c("0:0:0", "0:0:0", "5:43:42 pm", "5:43:52 pm", 
                        "0:0:0", "6:43:52 pm"), c2=LETTERS[1:6])
DT[Time=="0:0:0", Time := NA]
DT[, Time := as.POSIXct(Time, format = "%I:%M:%S %p")]
i <- which(is.na(DT$Time))
DT[, idx:=cumsum(!is.na(Time))]
DT[, idx:=rev(seq(.N)), by=idx]
DT[, Time := nafill(Time, type="nocb")]
DT[i, Time:=Time - (10*idx)][, idx:=NULL]
DT[, Time := format(Time, '%I:%M:%S %p')][]
#>           Time c2
#> 1: 05:43:22 PM  A
#> 2: 05:43:32 PM  B
#> 3: 05:43:42 PM  C
#> 4: 05:43:52 PM  D
#> 5: 06:43:42 PM  E
#> 6: 06:43:52 PM  F

Created on 2020-12-30 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM