Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

Dates Mysteriously Converted to Numeric Data Once Put Into Data Frame

$
0
0

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?


Viewing all articles
Browse latest Browse all 205372

Trending Articles