0

I'm a new to R. I have an Excel file called "bitcoin" which contains two columns: "date" and "BTC.Close". I've imported the data into R and transformed it into an xts object. However, I'm encountering an issue when attempting to compute returns using the following code:

library(quantmod)
ret_btc <- diff(log(Cl(bitcoin)))

This is resulting in an error message:

Error in log(Cl(bitcoin)) : non-numeric argument to mathematical function.

My objective is to generate a return series in the form of an xts object. I would greatly appreciate any assistance with this matter.

I transformed the file into an xts format using the following code:

btc$date <- as.POSIXct(btc$date, format = "%Y-%m-%d %H:%M:%S")
bitcoin <- xts(btc, order.by = btc$date)
str(bitcoin)
## An xts object on 2020-01-01 / 2021-12-31 23:00:00 containing:
## Data:    character [17544, 2]
## Columns: date, BTC.Close
## Index:    POSIXct, POSIXt [17544] (Time Zone: "UTC")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
ines
  • 1
  • 2
  • 2
    Welcome to SO, ines! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (you've got some, but be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Aug 22 '23 at 18:41
  • 2
    What package is the `Cl()` function from? Seconding ^, it will be easier if you can share, for instance, the output of running `dput(bitcoin)` so we can understand the data types and structure you are working with, and try out potential solutions on data similar to yours. – Jon Spring Aug 22 '23 at 19:10
  • 2
    My guess is that `bitcoin` contains character data, not numeric. Use `str(bitcoin)` to check. It would also help if you give us the code you used to import the data into R and how you created the xts object. – Joshua Ulrich Aug 22 '23 at 19:21
  • `str(bitcoin)` shows the data is character. It would help if you give us the code you used to import the data into R. It's either that, or how the data is stored in the file you're importing. – Joshua Ulrich Aug 22 '23 at 21:16

0 Answers0