0

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

  • 1
    # Hi mineralpoint, welcome to Stack Overflow. I think you have a typo on your first line where you assign `df`. It will be much easier to help if you provide at least a sample of your data with `dput(data[1:30,])`. You can edit your question and paste the output. You can surround it with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – Ian Campbell May 08 '20 at 20:47
  • Two suggestions: 1) Copying the data from the `.csv` file doesn't help because we need to know exactly what the data structure is you're trying to work on. 2) You need to edit your original post to provide the data because comments have length and format limitations. If `df` is working, you could provide `dput(head(df))`. – Ian Campbell May 08 '20 at 21:14
  • Thanks, I just posted what I got from using dput(head(df)). – mineralpoint May 08 '20 at 21:21

1 Answers1

0

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.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57