简体   繁体   English

dim(X)必须为正长度(总和)

[英]dim(X) must have a positive length (cumsum)

I'm running the following section of code that includes a loop: 我正在运行以下包含循环的代码部分:

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)

the loop appears to run without issue (columns 1 and 2 are populated), however I receive the following error relating to the last line: 循环似乎没有问题地运行(已填充第1列和第2列),但是我收到与最后一行有关的以下错误:

Error in apply(CancCheck[, 2], 2, cumsum) : 
  dim(X) must have a positive length

The function should "Compute cumulative sums of columns of matrix". 该函数应“计算矩阵列的累加和”。 I'm not clear on what it defines as the "dim (x)" that it is not positive. 我不清楚它定义为“ dim(x)”是不是肯定的。

Below are the dput() for CancCheck and CancNP: 以下是CancCheck和CancNP的dput():

iForeper <- 200

CancCheck 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 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")

Thanks in advance. 提前致谢。

Apologies to those helpful responders for my delayed reply; 向那些乐于助人的回复者致歉,我延迟回复; this has now been solved. 现在已经解决了。

The aim was for column 3 of CancCheck ( CancCheck[,3] ) to produce a cumulative sum of the values in column 2 ( CancCheck[,2] ). 目的是使CancCheck( CancCheck[,3] )的第CancCheck[,3]列产生第2列( CancCheck[,2] )中值的累积和。 The first row of CancCheck contained NA values, prohibiting the function from working - thus it needed to be removed. CancCheck的第一行包含NA值,从而禁止该功能正常工作-因此需要将其删除。

Using CancCheck = CancCheck[-1,] the first row containing NA's was removed, leaving the required populated rows. 使用CancCheck = CancCheck[-1,] ,删除了包含NA的第一行,保留了所需的填充行。 As the user "etienne" mentioned previously, CancCheck[,3]<- cumsum(CancCheck[,2]) is the correct way to take the cumulative sum for a vector, and thus I achieved the desired output with the final code: 正如前面提到的用户“ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM