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?
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)
You can try this :
fulldf <- data.frame(day = sapply(mydata,'[[', 1), price = sapply(mydata, '[[',2))
fulldf$day <- as.Date(fulldf$day)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.