2017-12-29 21 views
1

병합하려고하는 데이터 프레임이 두 개 있습니다 (d1small). 각 데이터 프레임을 내 보낸 후 here으로 만들었습니다.dplyr에서 POSIXct 객체의 full_join에 오류가 있습니다.

d1 데이터 프레임을 사용하여 small 데이터 프레임을 생성했습니다. 나는 일련의 for if 회 돌이를 사용하여 데이터 세트를 생성하기 위해 d1 데이터 세트에서 각 종 (sps)의 존재/부재 (2 시간 빈)를 결정했다.

내가 이런 식으로 뭔가를 얻을 수 d1와 (가상 예)를 small에서 TRUE/FALSE 행을 병합 할 일은 노력하고 있어요 :

  datetime  MUVI80 MUXX80 MICRO80 TAHU80 TAST80 ERDO80 LEAM80 ONZI80 MEME80 MAMO80 sps pp  datetime  km crossingtype 
1 2012-06-19 01:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE MICRO 0 2012-06-19 02:19 80 Exploration 
2 2012-06-21 21:42:00 FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE MUXX 1 2012-06-21 23:23 80  Unknown 
3 2012-07-15 09:42:00 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE MAMO 0 2012-07-15 11:38 80  Complete 
4 2012-07-20 21:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE MICRO 0 2012-07-20 22:19 80 Exploration 
5 2012-07-29 21:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE MICRO 0 2012-07-29 23:03 80 Exploration 
6 2012-08-08 23:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE MICRO 0 2012-08-07 02:04 80  Complete 

두 데이터 세트가 공통 필드 datetime을 공유하는 동안, 그들은 다른 포맷이 두 가지 이유에 대한 문제를 일으키는 :

  1. datetime 필드에서 POSIXct 개체입니다이지만 d1에는 없습니다.
  2. smalldatetime 필드를 만들려면 2 시간의 휴지통도 만들었습니다. 즉, 2 시간 내에 존재하는 종 (TRUE) 또는 결석 (FALSE)이었습니다. 즉, datetime 필드는 smalld1 데이터 세트간에 정확히 일치하지 않습니다. 대신 datetime 필드는 d1이고 datetime 필드는 2 시간 이내에 small입니다. 따라서

, 내가하려고하면

time<-dplyr::full_join(small, d1, by = "datetime") 

그것은 분명 작동하지 않습니다. 다른 datetime 필드 다음에있는 포맷 어떤

  1. 확인이 그들을 강제 :

    Error in full_join_impl(x, y, by$x, by$y, suffix$x, suffix$y, check_na_matches(na_matches)) : cannot join a POSIXct object with an object that is not a POSIXct object 
    

    사람이 내가 할 수있는 방법에 대한 제안 사항이 있습니까 다음과 같이

    내가 오류입니다 동일한 형식.

  2. datetime 필드에서 시간이 일치하지 않아도이 두 데이터 세트를 병합합니다.
+0

두 데이터 세트를 공유 할 수 있습니까? – suchait

답변

2

패키지는 range 기반의 data.frame 및 테이블 조인 시나리오를 유연하게 처리 할 수 ​​있습니다. OP에서 언급 한 문제를 해결하기 위해 sqldf을 사용할 수있는 방법을 설명하겠습니다.

I started with reading data from files shared in OP. 

library(sqldf) 

# Read the data from d1.txt. Pretty straight forward. 
d1 <- read.table("d1.txt", header = TRUE, stringsAsFactors = FALSE) 

# The datetime column is character. Hence change it to POSIXct 
d1$datetime <- as.POSIXct(d1$datetime) 

# small.txt file doesn't contain datetime together. Need to introduce 
# another column as onlytime to read time part separately. 
small <- read.table("small.txt", header = TRUE, stringsAsFactors = FALSE) 

# merge onlytime part with date part in datetime column 
small$datetime = paste(small$datetime, small$onlytime, sep = " ") 
# drop column onlytime 
small$onlytime <- NULL 
# Now datetime column is character. Hence change it to POSIXct 
small$datetime <- as.POSIXct(small$datetime) 

# everything is ready now. Lets join two dataframes 
# small$datetime is at 2 hours interval and represent data for past 2 hours 
# Hence range matching records to be found within 2 hours(2*60*60) before and 
# time of current row 

time = sqldf("select * from d1 
       inner join small 
       on d1.datetime between (small.datetime - 2*60*60) and small.datetime") 


head(time, 3) 
    ID  date sps time pp   datetime km crossingtype   datetime MUVI80 MUXX80 MICRO80 TAHU80 TAST80 ERDO80 LEAM80 ONZI80 
1 15185 2012-10-22 MICRO 3:42 0 2012-10-22 03:42:00 80  Unknown 2012-10-22 03:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE 
2 15187 2012-10-23 MICRO 0:40 0 2012-10-23 00:40:00 80  Unknown 2012-10-23 01:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE 
3 17018 2012-10-29 MICRO 21:03 0 2012-10-29 21:03:00 80  Unknown 2012-10-29 21:42:00 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE 

가입 유형은 OP의 실제 개체에 맞게 변경할 수 있습니다.

+1

이것은 완벽합니다. 그것은 내가 필요한 것을 정확하게합니다. 분명한 대답을 가져 주셔서 감사합니다! –