按动物园断点的日期过滤数据帧
Filtering dataframes by dates for zoo breakpoints
我有以下 CSV 文件:
f , c
1,19/11/2014 12:00,0.01
2,19/11/2014 13:00,0.01
3,20/11/2014 15:00,0.01
4,20/11/2014 16:00,0.01
5,20/11/2014 17:00,0.01
6,20/11/2014 19:00,0.01
7,20/11/2014 22:00,0.20
8,20/11/2014 23:00,0.03
9,21/11/2014 16:00,0.01
10,21/11/2014 17:00,0.01
我使用以下方法读取 CSV 文件:
library(strucchange)
ts1<-read.csv (file.choose())
我想过滤给定日期(DATE1 和 DATE2)之间的所有行。
DATE1 <- as.Date("20/11/2014 16:00", format = "%d/%m/%Y %H:%M")
DATE2 <- as.Date("20/11/2014 23:00", format = "%d/%m/%Y %H:%M")
因此,我将在数据框中获得以下行 ts2
:
f , c
3,20/11/2014 15:00,0.01
4,20/11/2014 16:00,0.01
5,20/11/2014 17:00,0.01
6,20/11/2014 19:00,0.01
7,20/11/2014 22:00,0.20
为了在 ts1 和 ts2 之间进行转换,我尝试了以下操作:
ts1$f<-as.Date(ts1$f, format = "%d/%m/%Y %H:%M")
ts2<-ts1[ts1$f %in% DATE1:DATE2, ]
ts2$f<-as.factor (ts2$f)
之后我将能够使用以下内容:
z<-read.zoo(ts2, tz = "", format = "%d/%m/%Y %H:%M", sep = ",")
bp <- breakpoints(z ~ 1, h = 2)
但我收到以下错误:
> bp <- breakpoints(z ~ 1, h = 2)
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 11L, 6L, 3L, 4L, :
replacement has 10 rows, data has 5
使用 R 时要记住的一件事是日期 class 仅适用于日期而不适用于时间。因此在这种情况下,strptime 或 as.POSIXct 是要使用的函数,而不是 as.Date。同样在你将 date/time 转换为 POSIX 对象时,我建议不要转换回一个因子。
#Enter the data
ts1<-read.table(header = TRUE, sep=",", text="row, f , c
1,19/11/2014 12:00,0.01
2,19/11/2014 13:00,0.01
3,20/11/2014 15:00,0.01
4,20/11/2014 16:00,0.01
5,20/11/2014 17:00,0.01
6,20/11/2014 19:00,0.01
7,20/11/2014 22:00,0.20
8,20/11/2014 23:00,0.03
9,21/11/2014 16:00,0.01
10,21/11/2014 17:00,0.01 ")
#convert to date/time
ts1$f<-as.POSIXct(ts1$f, format="%d/%m/%Y %H:%M")
#create limits
DATE1 <- as.POSIXct("20/11/2014 16:00", format = "%d/%m/%Y %H:%M")
DATE2 <- as.POSIXct("20/11/2014 23:00", format = "%d/%m/%Y %H:%M")
#subset data from between limits
ts2<-subset(ts1, f>=DATE1 & f<=DATE2)
我有以下 CSV 文件:
f , c
1,19/11/2014 12:00,0.01
2,19/11/2014 13:00,0.01
3,20/11/2014 15:00,0.01
4,20/11/2014 16:00,0.01
5,20/11/2014 17:00,0.01
6,20/11/2014 19:00,0.01
7,20/11/2014 22:00,0.20
8,20/11/2014 23:00,0.03
9,21/11/2014 16:00,0.01
10,21/11/2014 17:00,0.01
我使用以下方法读取 CSV 文件:
library(strucchange)
ts1<-read.csv (file.choose())
我想过滤给定日期(DATE1 和 DATE2)之间的所有行。
DATE1 <- as.Date("20/11/2014 16:00", format = "%d/%m/%Y %H:%M")
DATE2 <- as.Date("20/11/2014 23:00", format = "%d/%m/%Y %H:%M")
因此,我将在数据框中获得以下行 ts2
:
f , c
3,20/11/2014 15:00,0.01
4,20/11/2014 16:00,0.01
5,20/11/2014 17:00,0.01
6,20/11/2014 19:00,0.01
7,20/11/2014 22:00,0.20
为了在 ts1 和 ts2 之间进行转换,我尝试了以下操作:
ts1$f<-as.Date(ts1$f, format = "%d/%m/%Y %H:%M")
ts2<-ts1[ts1$f %in% DATE1:DATE2, ]
ts2$f<-as.factor (ts2$f)
之后我将能够使用以下内容:
z<-read.zoo(ts2, tz = "", format = "%d/%m/%Y %H:%M", sep = ",")
bp <- breakpoints(z ~ 1, h = 2)
但我收到以下错误:
> bp <- breakpoints(z ~ 1, h = 2)
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 11L, 6L, 3L, 4L, :
replacement has 10 rows, data has 5
使用 R 时要记住的一件事是日期 class 仅适用于日期而不适用于时间。因此在这种情况下,strptime 或 as.POSIXct 是要使用的函数,而不是 as.Date。同样在你将 date/time 转换为 POSIX 对象时,我建议不要转换回一个因子。
#Enter the data
ts1<-read.table(header = TRUE, sep=",", text="row, f , c
1,19/11/2014 12:00,0.01
2,19/11/2014 13:00,0.01
3,20/11/2014 15:00,0.01
4,20/11/2014 16:00,0.01
5,20/11/2014 17:00,0.01
6,20/11/2014 19:00,0.01
7,20/11/2014 22:00,0.20
8,20/11/2014 23:00,0.03
9,21/11/2014 16:00,0.01
10,21/11/2014 17:00,0.01 ")
#convert to date/time
ts1$f<-as.POSIXct(ts1$f, format="%d/%m/%Y %H:%M")
#create limits
DATE1 <- as.POSIXct("20/11/2014 16:00", format = "%d/%m/%Y %H:%M")
DATE2 <- as.POSIXct("20/11/2014 23:00", format = "%d/%m/%Y %H:%M")
#subset data from between limits
ts2<-subset(ts1, f>=DATE1 & f<=DATE2)