Search code examples
rdataframetime-seriesnumericxts

apply.weeky function is suddenly returning "'x' must be numeric" only when using "sum" or "colSums"


I've been importing data from a csv, then putting it into a dataframe. I then pad it, convert it to an XTS. After that I use apply.weekly to break it down into weekly data. The code is below.

library(padr)
library(xts)
library(tidyr)
library(seasonal)
library(forecast)]
library(lubridate)
library(tidyverse)
library(zoo)

df <- data.frame(data$f1.date[1:831], data.$f1[1:831])  #create data frame
colnames(df)<- c('date', 'f1')
df$date <- (as.Date(df$date, format='%m/%d/%Y'))
df<-pad(df)  # add missing dates


df<-xts(df, order.by = df$date)
df$t1<-replace_na(df$f1,0)  # Add Zeros for NA vals

dtw<-apply.weekly(df$f1,FUN=colSums) # I've tried using sum, colSums and mean.  Mean works fine.

However, sum or colSums returns the following.

Error in FUN(x[(INDEX[y] + 1):INDEX[y + 1]], ...) : 'x' must be numeric

So far, my only thought was to try df$t1<-as.numeric(df$t1), but that had no effect.

Does anyone have any suggestions on this? This exact script was working fine with the same csv file, and I don't think anything has changed. All I can think is that some package didn't get loaded.

A sample of my data is below, the full version has approximately 800 rows. I am working on making it reproducible now.

Using dput(head(df)) gives me this.

structure(c("2010-01-01", "2010-01-02", "2010-01-03", "2010-01-04", 
"2010-01-05", "2010-01-06", " 1", "0", "0", "0", "0", "0"), class = c("xts", 
"zoo"), index = structure(c(1262304000, 1262390400, 1262476800, 
1262563200, 1262649600, 1262736000), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
2L), .Dimnames = list(NULL, c("date", "f1")))

I also edited it to change 't1' to 'f1' for consistency.

Thanks


Solution

  • The problem is that your column f1 is of class character.

    is.character(df$f1)
    [1] TRUE
    

    One approach would be to convert it to numeric within your apply.weekly call:

    apply.weekly(df$f1,function(x){sum(as.numeric(x))})
               f1
    2010-01-03  1
    2010-01-06  0
    

    The reason for all this is because xts class objects hold the data inside a matrix. And matrices in R can only have columns of one class.