我正在运行以下代码部分,其中包括一个循环:
i <- 1 #defined as 1 earlier in the code
CancCheck <- data.frame() #Blank data frame
for (i in 1:iForeper+1) {
CancCheck[i,1]<-i-1
CancCheck[i,2]<- sum(CancNP[CancNP[,7]==i-1,4]) # aggregate all rooms with same cancellation window
}
CancCheck[,3]<- apply(CancCheck[,2],2,cumsum)
循环似乎运行时没有问题(填充了第1列和第2列),但是我收到了与最后一行有关的以下错误:
Error in apply(CancCheck[, 2], 2, cumsum) :
dim(X) must have a positive length
函数应"计算矩阵的列"。我不清楚它定义的"dim(x)"是什么,它不是正的。
以下是CancCheck和CancNP:的dput()
iForeper <- 200
CancCheck
>dput(head(CancCheck,20))
structure(list(V1 = c(NA, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19), V2 = c(NA, 1077L, 1713L, 2631L,
3204L, 3697L, 3802L, 3789L, 3784L, 3554L, 3170L, 3059L, 2989L,
2919L, 2676L, 2608L, 2281L, 2340L, 2164L, 2137L), V3 = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("V1",
"V2", "V3"), row.names = c(NA, 20L), class = "data.frame")
CancNP
> dput(head(CancNP,20))
structure(list(bookingdata.Cancellation.Date = structure(c(16036,
16036, 16036, 16036, 16036, 16031, 16031, 16031, 16031, 16031,
16031, 16031, 16031, 15986, 15986, 15986, 15986, 15986, 15986,
15986), class = "Date"), bookingdata.Arrival.Date = structure(c(16070,
16068, 16058, 16052, 16049, 16052, 16049, 16043, 16039, 16038,
16037, 16036, 16033, 16022, 16021, 16007, 16002, 16016, 16006,
16003), class = "Date"), bookingdata.Creation.Date = structure(c(16027,
16027, 16027, 16027, 16027, 16028, 16028, 16028, 16028, 16028,
16028, 16028, 16028, 15986, 15986, 15986, 15986, 15986, 15986,
15986), class = "Date"), bookingdata.Room.nights = c(37L, 37L,
37L, 37L, 37L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 31L, 31L,
31L, 31L, 31L, 31L, 31L), CFBMD = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), FBWRMD = structure(c(-1,
-3, -13, -19, -22, -19, -22, -28, -32, -33, -34, -35, -38, -49,
-50, -64, -69, -55, -65, -68), class = "difftime", units = "days"),
V7 = structure(c(34, 32, 22, 16, 13, 21, 18, 12, 8, 7, 6,
5, 2, 36, 35, 21, 16, 30, 20, 17), class = "difftime", units = "days")), .Names = c("bookingdata.Cancellation.Date",
"bookingdata.Arrival.Date", "bookingdata.Creation.Date", "bookingdata.Room.nights",
"CFBMD", "FBWRMD", "V7"), row.names = c(202L, 203L, 204L, 205L,
206L, 257L, 258L, 259L, 260L, 261L, 262L, 263L, 264L, 313L, 314L,
315L, 316L, 317L, 318L, 319L), class = "data.frame")
提前谢谢。
为我延迟回复向那些乐于助人的回复者道歉;这个问题现在已经解决了。
目的是使CancCheck(CancCheck[,3]
)的第3列产生第2列(CancCheck[,2]
)中的值的累积和。CancCheck的第一行包含NA值,禁止函数工作,因此需要将其删除。
使用CancCheck = CancCheck[-1,]
删除了包含NA的第一行,留下了所需的填充行。正如前面提到的用户"etienne"所说,CancCheck[,3]<- cumsum(CancCheck[,2])
是获取向量累积和的正确方法,因此我用最终代码实现了所需的输出:
i <- 1 #defined as 1 earlier in the code
CancCheck <- data.frame()
for (i in 1:iForeper+1) {
CancCheck[i,1]<-i-1
CancCheck[i,2]<- sum(CancNP[CancNP[,7]==i-1,4]) # % aggregate all rooms with same cancellation window
}
CancCheck = CancCheck[-1,] #Removes first row, containing NA values
CancCheck[,3] <- cumsum(CancCheck[,2]) #Cumulative Density`
> head(CancCheck)
V1 V2 V3
2 1 1077 1077
3 2 1713 2790
4 3 2631 5421
5 4 3204 8625
6 5 3697 12322
7 6 3802 16124