0

I have a xts list that looks like this (my list is called prices):

head(prices[[1]])

           X        ID      Name            Price              Shares.Outstanding
1980-03-31 "170481" "55160" "Donec Non Ltd" "5,72762874283858" "973,74375"       
1980-04-30 "170482" "55160" "Donec Non Ltd" "7,60702431506945" "973,74375"       
1980-05-30 "170483" "55160" "Donec Non Ltd" "6,97838217177628" "973,74375"       
1980-06-30 "170484" "55160" "Donec Non Ltd" "8,24558297069908" "973,74375"       
1980-07-31 "170485" "55160" "Donec Non Ltd" "7,92929698195742" "973,74375" 

I would like to multiply the price with the shares outstanding of a specific day. I tried to do it like this:

day <- prices[[1]]["1980-06-30"]
market_value <- coredata(day[,4]) * coredata(day[,5])

However, it gives me the error: non-numeric argument for binary operator. I also tried using as.numeric instead of coredata but that did not work either.

Thank you!

Katharina Böhm
  • 125
  • 1
  • 8

1 Answers1

0

You are trying to multiply characters, that's why R is throwing an error.

First, you should change those variables to numeric, and the comma must be replaced for a dot:

library(dplyr)

prices[[1]] <- prices[[1]] %>% mutate(Price = as.numeric(sub(",", ".", Price, fixed = TRUE)), 
                                      Shares.Outstanding = as.numeric(sub(",", ".", Shares.Outstanding, fixed = TRUE)))
    

Then you can multiply them!

David Jorquera
  • 2,046
  • 12
  • 35