Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 添加缺少的行_R_Posixct - Fatal编程技术网

R 添加缺少的行

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”中编号为零的其他行添加到数据框中。输出示例:

我的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: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”)
或博客文章了解其工作原理。

它仅在数据中有class
Date
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)