as.Date NA error and format invalid 'trim' argument

Hi guys

I cant do the format that i want to my data

as.Date(price$date, "Y%-%m-%d")

NA error

format(price$date, "Y%-%m-%d")
#invalid 'trim' argument

I have a problem with locale i guess

Other information about my data:

class(price)
[1] "data.frame"

str(price)
'data.frame': 19285 obs. of 3 variables:
crypto: Factor w/ 40 levels "ADA","ALGO","ATOM",..: 34 12 38 32 18 39 1 5 20 31 ... price : num 11194 389.8 0.3 10005 98602 ...
$ date : Factor w/ 1040 levels "01-01-2018","01-01-2019",..: 151 151 151 151 151 151 151 151 151 151 ...


DATE

You have to tell as.Date() what format the date is in, not the final format you want. If it is currently d-m-Y, use

as.Date(price$date, "%d-%m-%Y")

Hi

I can't transform the data frame to a times series

class(price2)
str(price2)

price2$date <- as.Date(price2$date)

as.Date(price2$date, "Y%-%m-%d")

xts_price2 <- xts(price2$crypto, price2$price, price2$date)

xts_price2 <- xts(select(price2,-c("crypto","date")),
order.by = as.POSIXct(strptime(price2$date,"%Y-%m-%d")))

Errors:

Error in xts(price2$crypto, price2$price, price2$date) :
order.by requires an appropriate time-based object

xts_price2 <- xts(select(price2,-c("crypto","date")),

  •               order.by = as.POSIXct(strptime(price2$date,"%Y-%m-%d")))
    

Error in xts(select(price2, -c("crypto", "date")), order.by = as.POSIXct(strptime(price2$date, :
'order.by' cannot contain 'NA', 'NaN', or 'Inf'

Here the photos of data frame
data2

Let's make sure your original problem of using as.Date is solved. If you original date column is in the format d-m-y, you need to use code like

price$date <- as.Date(price$date, "%d-%m-%Y")

Running str(price) after that should show you that the conversion to a numeric date was successful. Can you confirm that?

yes now is ok i guess

The following command will not change the values in price$data to be dates.

as.Date(price$date, "%d-%m-%Y")

It will only display the result of that action on the console. To store the result in the price data frame, you have to do

price$date <- as.Date(price$date, "%d-%m-%Y")

ok, but now i cant transform the data frame to a xts

it doesn't work with the format %d-%m-%Y or %Y%-%m-%d

I have never used the xts() function, so treat my advice with care.
The documentation of the xts() function shows the following

xts(x = NULL,
    order.by = index(x),
    frequency = NULL,
    unique = TRUE,
    tzone = Sys.getenv("TZ"),
    ...)

The order.by argument is described as "a corresponding vector of unique times/dates".
One thing you have tried is

xts_price <- xts(price$crypto, price$price, price$date)

That assigns price$price to the order.by argument and since price$price is not a "vector of unique times/dates", you get the error that "order.by requires an appropriate time-based object".
Your second attempt is

    xts_price2 <- xts(select(price,-c("crypto","date")), 
                       order.by = as.POSIXct(strptime(price$date,"%Y-%m-%d")))

where you are trying to transform price$date. But price$date is already a Date object and does not need to be transformed. My guess - remember I have never used this function - is

xts_price2 <- xts(x = price$price, order.by = price$date)

And looking at the image of your data, I would guess that you need to work with a single value of the Crypto column, but I really do not know what you are trying to do and I do not work with time series data.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.