【发布时间】:2017-02-14 18:41:47
【问题描述】:
我正在尝试合并几个数据集。但是,每个都有不规则的每小时时间戳。我的目标是在同一小时间隔内合并数据,并填写常规时间序列时间表。例如,您可以看到两个数据集:
x <- structure(list(Date = structure(1:5, .Label = c("09.09.2011 21:54",
"09.09.2011 22:59", "09.10.2011 00:04", "09.10.2011 01:09", "09.10.2011 02:14"
), class = "factor"), hexane = c(0, 0, 0, 0, 0), benzene = structure(c(1L,
2L, 4L, 3L, 5L), .Label = c("0", "4.4", "4.7", "6.3", "7.7"), class = "factor"),
toluene = c(2.2, 2.6, 3.5, 2.7, 3.1)), .Names = c("Date",
"hexane", "benzene", "toluene"), row.names = c(NA, 5L), class = "data.frame")
>
y <- structure(list(Date = structure(1:5, .Label = c("09.09.2011 21:54",
"09.09.2011 22:59", "09.10.2011 00:04", "09.10.2011 01:09", "09.10.2011 02:14"
), class = "factor"), ethane = c(14.4, 868.9, 547, 491.4, 56.1
), propane = c(6.4, 32.1, 23.7, 22.8, 7.2), isobutane = c(1.7,
2, 1.8, 1.3, 1.1), n.butane = c(3.1, 3, 3.7, 4.3, 2.9), isopentane = c(5.6,
3, 2.4, 3.4, 2.7), n.pentane = c(1.4, 2.4, 2.3, 2.4, 2.3)), .Names = c("Date",
"ethane", "propane", "isobutane", "n.butane", "isopentane", "n.pentane"
), row.names = c(NA, 5L), class = "data.frame")
na.fill (x, NA)
na.fill (y, NA
)
#identify "Date" column
x <- as.POSIXct(x$Date,format='%m.%d.%y %H:%M')
y <- as.POSIXct(y$Date,format='%m.%d.%y %H:%M')
#merge two data sets
merged_data <- merge.data.frame(x, y, by='Date', all=TRUE)
但是,输出文件“merged_data”上的日期列填充了 NA。我需要在 Date 列上设置一个每小时固定的时间戳。
【问题讨论】:
-
无论是我还是您提供的输入数据都遗漏了一些东西。 merge_date$Date 列用 NA 填充,因为 x$Date 和 y$Dat 以 NA 开头。
-
@GGamba 抱歉,我现在修好了。
标签: r