How to search for values ​​in R

Can someone help, I have a data frame ( set_rise) with each row containing the sunset time of this day and sunrise time the next day. I have a second data frame ( data) with a date / time column. I want to create a second column in datawith a letter corresponding to day or night, taking the date / time in dataand checking if it exists or not between any times in set_rise.

#df1- sunset, sunrise times
set_rise
                  set                rise 
1 2013-03-01 18:28:00 2013-03-02 08:27:00   
2 2013-03-02 18:31:00 2013-03-03 08:23:00    
3 2013-03-03 18:35:00 2013-03-04 08:19:00  
4 2013-03-04 18:38:00 2013-03-05 08:15:00   
5 2013-03-05 18:42:00 2013-03-06 08:12:00  
6 2013-03-06 18:45:00 2013-03-07 08:08:00   

#df2 my data    
  timedate
1 2013-03-01 19:00:00
2 2013-03-03 10:00:00
3 2013-03-06 00:01:00

I would like to deduce like this

data
timedate night_day
2013-03-01 19:00:00  N
2013-03-03 10:00:00  D
2013-03-06 00:01:00  N

Output Power (set_rise)

dput(set_rise)
structure(list(set = structure(list(sec = 0, min = 28L, hour = 18L, 
mday = 1L, mon = 2L, year = 113L, wday = 5L, yday = 59L, 
isdst = 0L, zone = "WET", gmtoff = NA_integer_), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
 "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), rise = structure(list(
sec = 0, min = 27L, hour = 8L, mday = 2L, mon = 2L, year = 113L, 
wday = 6L, yday = 60L, isdst = 0L, zone = "WET", gmtoff = NA_integer_), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), night = "N"), .Names = c("set", 
"rise", "night"), row.names = 1L, class = "data.frame")

Exiting dput (data)

dput(data)
structure(list(timedate = structure(c(1362873600, 1362960000, 
1364342400), class = c("POSIXct", "POSIXt"))), .Names = "timedate",     row.names = c(NA, 
-3L), class = "data.frame")
+4
source share
3 answers

, . set_rise POSIXct ( POSIXlt). numeric , . findInterval, , data: , , . :

#convert to POSIXct
set_rise[]<-lapply(set_rise,as.POSIXct)
#combine all the numeric values together
intervals<-c(t(matrix(c(as.numeric(set_rise$set),as.numeric(set_rise$rise)),ncol=2)))
#call findInterval and set the values, checking the parity
c("D","N")[1+(findInterval(as.numeric(data$timedate),intervals) %% 2)]
#[1] "N" "D" "N"
+1

, , :

check_night() data$timedate, / set_rise.

timedate <- c('2013-03-10 19:00:00', '2013-03-11 10:00:00', '2013-03-27 00:01:00')
data <- data.frame(timedate)
data$timedate <- as.POSIXct(data$timedate)

check_night <- function (t) {
  night <- "D"
  for (i in 1:length(rownames(set_rise))) {
    if ((t > set_rise[i,"set"]) && (t < set_rise[i,"rise"]))
      night <- "N"
  }
  return(night)
}

dplyr::mutate(.data = data, night=sapply(data$timedate, FUN = check_night))

# Output
             timedate night
1 0013-03-10 19:00:00     D
2 0013-03-11 10:00:00     N
3 0013-03-27 00:01:00     D

. , for.

+1

I have a similar solution for maj, I just nested the loops instead of doing the function and sapply. I also used strptime instead of as.POSIXct because it was giving me NA for some reason. If you have a large data set, go with the execution of the function and use sapply or lapply, it is easier to read and, most likely, faster. I am posting this to give you some options, May is probably a little faster and more elegant than mine.

df1<-read.csv("~/mysrc/data/sunsethelp.csv", header=T, stringsAsFactors=F)
df1$set<-strptime(df1$set, "%m/%d/%Y %H:%M")
df1$rise<-strptime(df1$rise, "%m/%d/%Y %H:%M")

timedate<-c('2013-03-01 19:00:00', '2013-03-03 10:00:00', '2013-03-06     00:01:00')
df2 <- data.frame(timedate)
df2$timedate <- strptime(timedate, "%Y-%m-%d %H:%M")

for(i in seq(nrow(df2))){
  for(j in seq(nrow(df1))){
    df2$night_day[i]<-ifelse(df2$timedate[i]>df1$set[j] && df2$timedate[i]    <df1$rise[j], "N", "D")
    if(df2$night_day[i]=="N")
      break
  }
}

#Output
             timedate night_day
1 2013-03-01 19:00:00         N
2 2013-03-03 10:00:00         D
3 2013-03-06 00:01:00         N
0
source

All Articles