图像 OCR 很好,但请了解如何使用 dput() 以便人们更轻松地为您提供帮助。
另外:您问题中的图片 1 与图片 2 的区别不仅仅是颜色。您修改了与 R 或 R 知识无关的图像之间的时间,并且确实没有帮助/令人困惑。因此,重申建议仅将dput 的输出用于代码块。
0 外部依赖库 R 解决方案:
read.csv(text="temp1,time1,temp2,time2
21.875,01.11.18 01:54,22.500,01.11.18 01:40
21.875,01.11.18 01:57,22.563,01.11.18 01:41
21.813,01.11.18 01:58,22.563,01.11.18 01:51
21.875,01.11.18 01:59,22.625,01.11.18 01:52
21.875,01.11.18 02:03,22.563,01.11.18 01:53
21.813,01.11.18 02:04,22.625,01.11.18 01:54
21.875,01.11.18 02:05,22.625,01.11.18 02:05
21.813,01.11.18 02:06,22.688,01.11.18 02:06",
stringsAsFactors=FALSE) -> xdf
xdf$time1 <- as.POSIXct(xdf$time1, format="%m.%d.%y %H:%M")
xdf$time2 <- as.POSIXct(xdf$time2, format="%m.%d.%y %H:%M")
setNames(
merge(xdf[,1:2], xdf[,3:4], by.x="time1", by.y="time2"),
c("time", "temp1", "temp2")
)
## time temp1 temp2
## 1 2018-01-11 01:54:00 21.875 22.625
## 2 2018-01-11 02:05:00 21.875 22.625
## 3 2018-01-11 02:06:00 21.813 22.688
57编译?依赖tidyverse解决方案:
read.csv(text="temp1,time1,temp2,time2
21.875,01.11.18 01:54,22.500,01.11.18 01:40
21.875,01.11.18 01:57,22.563,01.11.18 01:41
21.813,01.11.18 01:58,22.563,01.11.18 01:51
21.875,01.11.18 01:59,22.625,01.11.18 01:52
21.875,01.11.18 02:03,22.563,01.11.18 01:53
21.813,01.11.18 02:04,22.625,01.11.18 01:54
21.875,01.11.18 02:05,22.625,01.11.18 02:05
21.813,01.11.18 02:06,22.688,01.11.18 02:06",
stringsAsFactors=FALSE) -> xdf
library(tidyverse)
mutate(xdf, time1 = lubridate::mdy_hm(time1)) %>%
mutate(time2 = lubridate::mdy_hm(time2)) -> xdf
left_join(
select(xdf, temp1, time1),
select(xdf, temp2, time2),
by = c("time1" = "time2")
) %>%
filter(!is.na(temp2)) %>%
select(time = time1, temp1, temp2)
## time temp1 temp2
## 1 2018-01-11 01:54:00 21.875 22.625
## 2 2018-01-11 02:05:00 21.875 22.625
## 3 2018-01-11 02:06:00 21.813 22.688