我有以下列表,“日期”,表示日期和表示小时数的相关值的数字:
$`2012-08-01 20:05:37`
214 216
$`2012-08-01 22:05:32`
211
我有一个数据框,“数据”,其中每行包含两组日期(“小时”和“时间点”)和两个日期之间的小时差异(“差异”)。我需要遍历“list”的每个元素,并在数据框“data”中找到与向数字添加列表值相关联的相应“time_point”。例如,与“list”中的第一个元素关联的第一个值应与“time_point”“2012-08-10 17:53:16”匹配,因为214小时已添加到该日期的数字结果中。输出可以是日期的列表或数据框。有关如何做到这一点的任何想法?
diff hour time_point ID
70 214 2012-08-01 20:05:37 2012-08-10 17:53:16 18
71 215 2012-08-01 20:05:37 2012-08-10 18:53:21 18
72 216 2012-08-01 20:05:37 2012-08-10 19:53:16 18
73 217 2012-08-01 20:05:37 2012-08-10 20:53:21 18
74 218 2012-08-01 20:05:37 2012-08-10 21:54:51 18
75 219 2012-08-01 20:05:37 2012-08-10 22:53:31 18
218 206 2012-08-02 02:05:12 2012-08-10 15:53:16 24
316 200 2012-08-02 06:50:17 2012-08-10 14:53:16 28
490 53 2012-08-02 22:49:52 2012-08-05 03:50:18 44
491 54 2012-08-02 22:49:52 2012-08-05 04:50:48 44
如果我正确地理解你,你正在寻找这个吗?
ll <- list("date1"=c(214,216),"date2" = 211)
f <- function(x) d[d[,"diff"] %in% x,"time_point"]
lapply(ll,f)
数据
d <- "diff hour time_point ID
214 '2012-08-01 20:05:37' '2012-08-10 17:53:16' 18
215 '2012-08-01 20:05:37' '2012-08-10 18:53:21' 18
216 '2012-08-01 20:05:37' '2012-08-10 19:53:16' 18
217 '2012-08-01 20:05:37' '2012-08-10 20:53:21' 18
218 '2012-08-01 20:05:37' '2012-08-10 21:54:51' 18
219 '2012-08-01 20:05:37' '2012-08-10 22:53:31' 18
206 '2012-08-02 02:05:12' '2012-08-10 15:53:16' 24
200 '2012-08-02 06:50:17' '2012-08-10 14:53:16' 28
53 '2012-08-02 22:49:52' '2012-08-05 03:50:18' 44
54 '2012-08-02 22:49:52' '2012-08-05 04:50:48' 44"
d <- read.table(text = d,header = T)