简体   繁体   English

从一行中减去秒并将结果设置为 R 中的另一行

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

I have a dataframe where the first column is Time.我有一个 dataframe,其中第一列是时间。 My current output looks like:我当前的 output 看起来像:

0:0:0 0:0:0
0:0:0 0:0:0
5:43:42 pm下午 5 点 43 分 42 秒
5:43:52 pm下午 5 点 43 分 52 秒

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.我正在尝试让我的代码的一部分搜索其中包含 0:0:0 值的任何行,并从其中包含值的最近行中减去 10 秒。 The desired outcome would look like:期望的结果如下所示:

5:43:22 pm下午 5 点 43 分 22 秒
5:43:32 pm下午 5 点 43 分 32 秒
5:43:42 pm下午 5 点 43 分 42 秒
5:43:52 pm下午 5 点 43 分 52 秒

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?对 go 关于创建这个有什么建议吗?

Assuming that the last value in your data would not be '0:0:0' you can do the following:假设您数据中的最后一个值不是'0:0:0' ,您可以执行以下操作:

Replace Time with NA where the value is 0:0:0 and turn them to POSIXct class.Time替换为值为0:0:0NA ,并将它们转换为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.通过从下一个值中减去 10 秒来替换NA值。

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.以我们需要的所需format (H:M:S) 更改POSIXct

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":这是一个data.table方法,假设您的意思是“具有值的最近行”,是不是“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)代表 package (v0.3.0) 于 2020 年 12 月 30 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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