R Stats:比较两个数据帧中的时间戳

R Stats:比较两个数据帧中的时间戳,r,comparison,timestamp,R,Comparison,Timestamp,我是新手,所以如果答案显而易见,请原谅我。我也试着寻找答案,但我认为我没有使用正确的术语 我有两个数据帧,每个数据帧由一个日期时间和一个值组成 e、 g。 数据帧1: 2003-01-01 10:00:00 | 10 2003-01-02 10:00:00 | 5 2003-01-03 10:00:00 | 7 ...<snip>... 2003-06-15 10:00:00 | 4.5 2003-06-16 10:00:00 | 4.5 2003-06-17 10:00:00 |

我是新手,所以如果答案显而易见,请原谅我。我也试着寻找答案,但我认为我没有使用正确的术语

我有两个数据帧,每个数据帧由一个日期时间和一个值组成

e、 g。 数据帧1:

2003-01-01 10:00:00 | 10
2003-01-02 10:00:00 | 5
2003-01-03 10:00:00 | 7
 ...<snip>...
2003-06-15 10:00:00 | 4.5
2003-06-16 10:00:00 | 4.5
2003-06-17 10:00:00 | 3.5
 ...<snip>...
2003-11-21 10:00:00 | 3.5
2003-11-22 10:00:00 | 4
2003-11-23 10:00:00 | 4.5
编辑2:

我让它和一个循环一起工作。有更好的方法吗

library(plyr)
library(lubridate)

df_measurements <- read.table(text = "
2003-01-01 10:00:00 | 10
2003-01-02 10:00:00 | 5
2003-01-03 10:00:00 | 7
2003-06-15 10:00:00 | 4.5
2003-06-16 10:00:00 | 4.5
2003-06-17 10:00:00 | 3.5
2003-11-21 10:00:00 | 3.5
2003-11-22 10:00:00 | 4
2003-11-23 10:00:00 | 4.5", sep = "|")

df_corrections <- read.table(text = "
2003-01-01 09:00:00 | 5.5
2003-05-01 09:00:00 | 6
2003-08-01 09:00:00 | 8", sep = "|")

#Create named columns and remove unneeded
df_measurements$time <- ymd_hms(df_measurements$V1)
df_measurements$obs <- df_measurements$V2
df_measurements$V1 <- NULL
df_measurements$V2 <- NULL

df_corrections$time <- ymd_hms(df_corrections$V1)
df_corrections$offset <- df_corrections$V2
df_corrections$V1 <- NULL
df_corrections$V2 <- NULL

#Get number of corrections
c_length <- nrow(df_corrections)

#Create blank data frame to merge results into
result <- data.frame(time=as.Date(character()), obs=numeric(), correction=numeric(), corrected=numeric(), stringsAsFactors=FALSE )

for(i in c(1:c_length)) {

  if(i < c_length) {

    subset_m <- df_measurements[df_measurements$time >= df_corrections$time[[i]] & df_measurements$time < df_corrections$time[[i+1]], ]
  } else {

    #Last correction in correction data frame
    subset_m <- df_measurements[df_measurements$time >= df_corrections$time[[i]], ]
  }

  #Make "correction" column and fill with correction to be used
  subset_m[, "correction"] <- rep(df_corrections$offset[[i]], nrow(subset_m)) 

  #Make "corrected" column and fill with corrected value
  subset_m$corrected <- subset_m$correction + subset_m$obs  

  #Combine subset with result
  result <- rbind(result, subset_m)

}

print(result)
库(plyr)
图书馆(lubridate)

df_测量值NB:这个答案是指原始问题,在我发布了一个工作答案后编辑的

这是你想要的吗

df <- read.table(text = "2003-01-01 10:00:00 | 10
2003-01-02 10:00:00 | 5
2003-01-03 10:00:00 | 7
2003-06-15 10:00:00 | 4.5
2003-06-16 10:00:00 | 4.5
2003-06-17 10:00:00 | 3.5", sep = "|")
df$time <- as.POSIXct(df$V1)

df2 <- read.table(text = "2003-01-01 09:00:00 | 2
2003-05-01 09:00:00 | 5", sep = "|")
df2$time <- as.POSIXct(df2$V1)

df$val <- with(df, ifelse(df$time >= df2$time[1] & df$time <= df2$time[2], df$V2 + 2, df$V2 + 5))

数据帧的内容只是一个例子。是的,您的答案会有效,但前提是我想比较数据帧1和两个时间戳。我将编辑我的问题,使之成为我想要的clearer@Takuya我回答了你发帖的原始问题,在你写的时候,我的回答起了作用。在我花时间提供了一个有效的答案之后,你改变了问题,我感到相当沮丧。不酷!这当然不是一种鼓励人们帮助你的行为。我很抱歉你有这种感觉,我真的很感谢人们花时间帮助我。也就是说,你的答案在技术上是正确的,因为它只有在比较两个变量时才起作用。另外,我对这个问题所做的唯一改变是增加示例数据的样本量,以澄清这个问题。你反对人们澄清他们提出的问题吗?请不要误解我的意思,我当然感谢那些澄清他们问题的人:请参考我对你最初问题的评论,我鼓励你这样做。我仍然不清楚你想要什么。
2003-05-01 09:00:00
从何而来?“最后三条记录位于数据帧2中的最后日期时间之后”。2003-11-21 10:00:00和2003-11-22 10:00:00以何种方式出现在2003-11-22 14:00:00之后?另一方面,2003-11-23 10:00:00确实在2003-11-22 14:00:00之后。但现在“+5规则”不适用,而是添加了3。请澄清你的问题,并确保你的例子是一致的。@Henrik我认为你看得不对。2003-10-20 14:00:00@Henrik意外地按了enter键,然后花了5分钟以上的时间编辑我的评论。我要修改我的问题instead@RJ,我做了一些修改来澄清这个问题。
 Time from DF1            Time from DF2      Calculation 
2003-11-21 10:00:00 >= 2003-10-20 14:00:00 = 3.5 + 2
2003-11-22 10:00:00 >= 2003-10-20 14:00:00 = 4   + 2
2003-11-23 10:00:00 >= 2003-11-22 14:00:00 = 4.5 + 3
library(plyr)
library(lubridate)

df_measurements <- read.table(text = "
2003-01-01 10:00:00 | 10
2003-01-02 10:00:00 | 5
2003-01-03 10:00:00 | 7
2003-06-15 10:00:00 | 4.5
2003-06-16 10:00:00 | 4.5
2003-06-17 10:00:00 | 3.5
2003-11-21 10:00:00 | 3.5
2003-11-22 10:00:00 | 4
2003-11-23 10:00:00 | 4.5", sep = "|")

df_corrections <- read.table(text = "
2003-01-01 09:00:00 | 5.5
2003-05-01 09:00:00 | 6
2003-08-01 09:00:00 | 8", sep = "|")

#Create named columns and remove unneeded
df_measurements$time <- ymd_hms(df_measurements$V1)
df_measurements$obs <- df_measurements$V2
df_measurements$V1 <- NULL
df_measurements$V2 <- NULL

df_corrections$time <- ymd_hms(df_corrections$V1)
df_corrections$offset <- df_corrections$V2
df_corrections$V1 <- NULL
df_corrections$V2 <- NULL

#Get number of corrections
c_length <- nrow(df_corrections)

#Create blank data frame to merge results into
result <- data.frame(time=as.Date(character()), obs=numeric(), correction=numeric(), corrected=numeric(), stringsAsFactors=FALSE )

for(i in c(1:c_length)) {

  if(i < c_length) {

    subset_m <- df_measurements[df_measurements$time >= df_corrections$time[[i]] & df_measurements$time < df_corrections$time[[i+1]], ]
  } else {

    #Last correction in correction data frame
    subset_m <- df_measurements[df_measurements$time >= df_corrections$time[[i]], ]
  }

  #Make "correction" column and fill with correction to be used
  subset_m[, "correction"] <- rep(df_corrections$offset[[i]], nrow(subset_m)) 

  #Make "corrected" column and fill with corrected value
  subset_m$corrected <- subset_m$correction + subset_m$obs  

  #Combine subset with result
  result <- rbind(result, subset_m)

}

print(result)
df <- read.table(text = "2003-01-01 10:00:00 | 10
2003-01-02 10:00:00 | 5
2003-01-03 10:00:00 | 7
2003-06-15 10:00:00 | 4.5
2003-06-16 10:00:00 | 4.5
2003-06-17 10:00:00 | 3.5", sep = "|")
df$time <- as.POSIXct(df$V1)

df2 <- read.table(text = "2003-01-01 09:00:00 | 2
2003-05-01 09:00:00 | 5", sep = "|")
df2$time <- as.POSIXct(df2$V1)

df$val <- with(df, ifelse(df$time >= df2$time[1] & df$time <= df2$time[2], df$V2 + 2, df$V2 + 5))