0

I have an xts object. Thus a time series of "outstanding share" of a company that are ordered by date. I want to multiply the time series of "outstanding shares" by the factor 7 in order to account for a stock split.

> outstanding_shares_xts <- shares_xts1[,1]
> adjusted <- outstanding_shares_xts*7

Error: Non-numeric argument for binary operator.

The ts "oustanding_shares_xts" is a column of integers. Does anyone has an idea??

  • Please check if your matrix in the xts is a numeric matrix and not a character matrix. This happens a lot when you do not exclude the date from the data.frame when creating the xts object. Otherwise, please include a sample of your data in the question with `dput(head(shares_xts1, 10))`. And read this on how to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – phiver Jun 20 '20 at 17:41

1 Answers1

1

My guess is that they may look like integers but are in fact not.

Sleuthing:

  • I initially thought it could be [-vs-[[ column subsetting, since tibble(a=1:2)[,1] does not produce an integer vector (it produces a single-column tibble), but tibble(a=1:2)[,1] * 7 still works.

  • Then I thought it could be due to factors, but it's a different error:

    data.frame(a=factor(1:2))[,1]*7
    # Warning in Ops.factor(data.frame(a = factor(1:2))[, 1], 7) :
    #   '*' not meaningful for factors
    # [1] NA NA
    
  • One possible is that you have character values that look like integers.

    dat <- data.frame(a=as.character(1:2))
    dat
    #   a
    # 1 1
    # 2 2
    dat[,1]*7
    # Error in dat[, 1] * 7 : non-numeric argument to binary operator
    

    Try converting that column to integer, something like

    str(dat)
    # 'data.frame': 2 obs. of  1 variable:
    #  $ a: chr  "1" "2"
    dat$a <- as.integer(dat$a)
    str(dat)
    # 'data.frame': 2 obs. of  1 variable:
    #  $ a: int  1 2
    dat[,1]*7
    # [1]  7 14
    
r2evans
  • 141,215
  • 6
  • 77
  • 149