2017-09-11 99 views
0

我一直在閱讀類似的文章我無法使任何解決方案適用於我的案例(可能是因爲我是R新手)。將數據框中的日期與其他數據框中的兩個日期進行比較

我有一個很長的數據集,有幾個參數,其中一個是日期,另一個數據框的日期間隔與特定的值相對應。我試圖讓重複的例子:

df = data.frame(date=c("2017/08/01 19:00:00","2017/08/01 19:10:00","2017/08/01 19:20:00","2017/08/01 19:30:00", 
        "2017/08/01 19:40:00","2017/08/01 19:50:00","2017/08/01 20:00:00","2017/08/01 20:10:00"), 
      factor1=c(10,15,11,13,14,12,16,15)) 

df2 = data.frame(start=c("2017/08/01 19:00:00","2017/08/01 19:40:00"), 
      end=c("2017/08/01 19:15:00","2017/08/01 20:05:00"), factor2=c("A","B")) 

df$date <- as.POSIXct(df$date) 
df2$start <- as.POSIXct(df2$start) 
df2$end <- as.POSIXct(df2$end) 

而結果我想是這樣的:

result = data.frame(date=c("2017/08/01 19:00:00","2017/08/01 19:10:00","2017/08/01 19:20:00","2017/08/01 19:30:00", 
        "2017/08/01 19:40:00","2017/08/01 19:50:00","2017/08/01 20:00:00","2017/08/01 20:10:00"), 
      factor1=c(10,15,11,13,14,12,16,15),factor2=c("A","A","NA","NA","B","B","B","NA")) 

我試着用ifelse:

ifelse(df$date >= df2$start & df$date <= df2$end,df2$factor2,"NA") 

但不能做這行得通。

有什麼建議嗎?

回答

1

該作品,以及在你的樣本數據:

result <- df 
result$factor2 <- NA 
for (i in seq_along(df$date)){ 
    p <- ifelse(length(grep("TRUE", (df$date[i] >= df2$start & df$date[i] <= df2$end)))!=0, 
       grep("TRUE", (df$date[i] >= df2$start & df$date[i] <= df2$end)), 
       NA) 
    result$factor2[i] <- ifelse(!is.na(p), 
          as.character(df2$factor2[p]), 
          "NA") 
    } 
print(result) 
#     date factor1 factor2 
#1 2017-08-01 19:00:00  10  A 
#2 2017-08-01 19:10:00  15  A 
#3 2017-08-01 19:20:00  11  NA 
#4 2017-08-01 19:30:00  13  NA 
#5 2017-08-01 19:40:00  14  B 
#6 2017-08-01 19:50:00  12  B 
#7 2017-08-01 20:00:00  16  B 
#8 2017-08-01 20:10:00  15  NA 
+0

它與原始數據完美配合!謝謝!!! <3 <3 – NeReiS

+0

乾杯!樂於幫助 –

0

我試着用在sqldfinner join和它似乎工作:

library(sqldf) 
df3 = sqldf("select df.*, df2.factor2 from df inner join df2 where df.date >= df2.start and df.date <= df2.end") 
result = merge(df, df3, by = "date", all.x = TRUE) 

(請注意,我也試圖與left outer join代替inner join,但是這給了我同樣的結果inner join ...它必須在sqldf僥倖)

+0

我不能」 t使它與原始數據一起工作: rsqlite_send_query(conn @ ptr,語句)錯誤:near「。」:語法錯誤 雖然也許是因爲我的數據有「。」在因素名稱中更改「。」爲「$」,但得到了然後 在rsqlite_send_query(conn @ ptr,語句)錯誤:附近「,」:語法錯誤 也許這個解決方案toooooo超過我的超級基本知識,以適應我的數據電視。 非常感謝您的回覆! :) – NeReiS

相關問題