根据这些日期向量
v<-c("2019-12-06 01:32:30 UTC","2019-12-31 18:44:31 UTC","2020-01-29 22:18:25 UTC","2020-03-22 16:44:29 UTC")
v<-as.POSIXct(v)
w<-c("2019-12-07 00:11:46","2020-01-01 05:29:45","2019-12-08 02:54:10","2020-03-23 07:48:26","2020-02-02 16:58:16","2020-01-31 06:46:46")
w<-as.POSIXct(w)
我想获得一个包含两列的数据帧。其中之一就是w
。第二个建立在v
条目上,使得在行中存在v
的最小日期,这使得差w-v
为正。例如,差异
w-rep(v[1],length(w))
Time differences in hours
[1] 22.65444 627.95417 49.36111 2598.26556 1407.42944 1349.23778
然后,如果所需数据帧的第二列是w
,则第一列在第一行具有日期2019-12-06 01:32:30 UTC
。操作应为:
date <- w-rep(v[1],length(w))
v[date==min(date[date>0])]
那么数据帧的第一行应该是
2019-12-06 01:32:30 UTC, 2019-12-07 00:11:46
如何使用循环构建其他行?
这个怎么样:
o <- outer(w, v, `-`)
o
# Time differences in hours
# [,1] [,2] [,3] [,4]
# [1,] 22.65444 -594.54583 -1294.11083 -2559.54528
# [2,] 627.95417 10.75389 -688.81111 -1954.24556
# [3,] 49.36111 -567.83917 -1267.40417 -2532.83861
# [4,] 2597.26556 1980.06528 1280.50028 15.06583
# [5,] 1407.42944 790.22917 90.66417 -1174.77028
# [6,] 1349.23778 732.03750 32.47250 -1232.96194
我们不想要负值,所以
o[o < 0] <- NA
o
# Time differences in hours
# [,1] [,2] [,3] [,4]
# [1,] 22.65444 NA NA NA
# [2,] 627.95417 10.75389 NA NA
# [3,] 49.36111 NA NA NA
# [4,] 2597.26556 1980.06528 1280.50028 15.06583
# [5,] 1407.42944 790.22917 90.66417 NA
# [6,] 1349.23778 732.03750 32.47250 NA
现在只需在每行上应用which.min
,然后在此值上子集v
:
apply(o, 1, which.min)
# [1] 1 2 1 4 3 3
v[apply(o, 1, which.min)]
# [1] "2019-12-06 01:32:30 EST" "2019-12-31 18:44:31 EST" "2019-12-06 01:32:30 EST" "2020-03-22 16:44:29 EDT"
# [5] "2020-01-29 22:18:25 EST" "2020-01-29 22:18:25 EST"
data.frame(w=w, v2=v[apply(o, 1, which.min)])
# w v2
# 1 2019-12-07 00:11:46 2019-12-06 01:32:30
# 2 2020-01-01 05:29:45 2019-12-31 18:44:31
# 3 2019-12-08 02:54:10 2019-12-06 01:32:30
# 4 2020-03-23 07:48:26 2020-03-22 16:44:29
# 5 2020-02-02 16:58:16 2020-01-29 22:18:25
# 6 2020-01-31 06:46:46 2020-01-29 22:18:25