Reputation: 2195
I am using an API to get some data. To get the data, I use:
library(httr)
data=GET(url, add_headers(Authorization=token))
mydata=content(data)$data
In a gross oversimplification, I then format all the data like so:
day=unlist(lapply(mydata,'[[', 1))
price=as.numeric(lapply(mydata, '[[',2))
fulldf=as.data.frame(cbind(day,price))
With str(fulldf)
I see that each column is factor data despite using as.numeric
. Documentation for ?factor
says "To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended..." So I use that as follows:
day=as.Date(levels(fulldf$day))[fulldf$day]
price=as.numeric(levels(fulldf$price))[fulldf$price]
fulldf=as.data.frame(cbind(day,price))
What is strange to me is that str(day)
shows a date vector as expected (format is "yyyy-mm-dd"), but str(fulldf$day)
shows a numeric vector. What am I doing wrong here? Is it something in an earlier step with wrapping lapply
in as.Date
or is it the as.data.frame
that is causing problems?
Upvotes: 0
Views: 53
Reputation: 3412
price
and date
are vectors when you call cbind
, so they become a matrix first. Matrices can't be of type Date
. They must be a primitive data type.
You could call the dataframe cbind method directly:
cbind.data.frame(day,price)
Or simpler, per @jay.sf:
data.frame(day, price)
Upvotes: 3
Reputation: 388972
You can try this :
fulldf <- data.frame(day = sapply(mydata,'[[', 1), price = sapply(mydata, '[[',2))
fulldf$day <- as.Date(fulldf$day)
Upvotes: 1