R 添加缺少的行
我的excel数据文件的格式为:R 添加缺少的行,r,posixct,R,Posixct,我的excel数据文件的格式为: day value 01-01-2000 00:00:00 4 01-01-2000 00:01:00 3 01-01-2000 00:02:00 1 01-01-2000 00:04:00 1 我用以下命令打开我的文件: ts = read.csv(file=pathfile, header=TRUE, sep=",") 如何将列“value”中编号为零的其他行添加到数据框中。输出示例:
day value
01-01-2000 00:00:00 4
01-01-2000 00:01:00 3
01-01-2000 00:02:00 1
01-01-2000 00:04:00 1
我用以下命令打开我的文件:
ts = read.csv(file=pathfile, header=TRUE, sep=",")
如何将列“value”中编号为零的其他行添加到数据框中。输出示例:
day value
01-01-2000 00:00:00 4
01-01-2000 00:01:00 3
01-01-2000 00:02:00 1
01-01-2000 00:03:00 0
01-01-2000 00:04:00 1
尝试:
请注意,您需要强制将第一列中的字符串作为字符而不是因子加载,否则您将遇到rbind问题。要使day列成为后面的一个因素,请执行以下操作:
ts.out$day = as.factor(ts.out$day)
我认为这是一个更通用的解决方案,它依赖于创建一个所有时间戳的序列,使用该序列作为新数据帧的基础,然后在适用的情况下在df中填充原始值
# convert original `day` to POSIX
ts$day <- as.POSIXct(ts$day, format="%m-%d-%Y %H:%M:%S", tz="GMT")
# generate a sequence of all minutes in a day
minAsNumeric <- 946684860 + seq(0,60*60*24,by=60) # all minutes of your first day
minAsPOSIX <- as.POSIXct(minAsNumeric, origin="1970-01-01", tz="GMT") # convert those minutes to POSIX
# build complete dataframe
newdata <- as.data.frame(minAsPOSIX)
newdata$value <- ts$value[pmatch(newdata$minAsPOSIX, ts$day)] # fill in original `value`s where present
newdata$value[is.na(newdata$value)] <- 0 # replace NAs with 0
#将原始'day'转换为POSIX
ts$day现在在padr
包中完全自动化。只需要一行代码
original <- data.frame(
day = as.POSIXct(c("01-01-2000 00:00:00",
"01-01-2000 00:01:00",
"01-01-2000 00:02:00",
"01-01-2000 00:04:00"), format="%m-%d-%Y %H:%M:%S"),
value = c(4, 3, 1, 1))
library(padr)
library(dplyr) # for the pipe operator
original %>% pad %>% fill_by_value(value)
原始%pad%>%fill\u by\u值(值)
查看vignette(“padr”)
或博客文章了解其工作原理。它仅在数据中有classDate
、POSIXct
或POSIXlt
变量时才起作用。如果时间维度是int
,它也可以工作吗?有一个函数padr::pad_int
。
original <- data.frame(
day = as.POSIXct(c("01-01-2000 00:00:00",
"01-01-2000 00:01:00",
"01-01-2000 00:02:00",
"01-01-2000 00:04:00"), format="%m-%d-%Y %H:%M:%S"),
value = c(4, 3, 1, 1))
library(padr)
library(dplyr) # for the pipe operator
original %>% pad %>% fill_by_value(value)