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:

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.

Below are the dput() for CancCheck and CancNP:

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

Thanks in advance.

Answers


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]). The first row of CancCheck contained NA values, prohibiting the function from working - thus it needed to be removed.

Using CancCheck = CancCheck[-1,] the first row containing NA's was removed, leaving the required populated rows. 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:

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

Need Your Help

(TCP) Server/Client program

c# iphone tcp client

I want to write (TCP) Server Client program.

Optimized strcmp implementation

c string-comparison strcmp

This function was found here. It's an implementation of strcmp: