Reputation: 339
I am working to read Census data into R using the agency's API, but I'm struggling to get it to read in the right format.
The URL that I'm trying to read in: https://api.census.gov/data/timeseries/eits/mwts?get=data_type_code,time_slot_id,seasonally_adj,category_code,cell_value,error_data&time=2012
And the format of that data if you just look at it in the web browser:
The closest I've gotten is with this series of code:
library(jsonlite)
data <- url("https://api.census.gov/data/timeseries/eits/mwts? get=data_type_code,time_slot_id,seasonally_adj,category_code,cell_value,error_data&time=2012")
datanew <- fromJSON(data)
But as you can see that's still not in a good dataframe format.
Is there an easier way that I'm missing? Thanks.
Upvotes: 0
Views: 220
Reputation: 462
I may be wrong but that API returns an improper use of JSON. Anyway, you can coerce datanew
to a data frame, but that leaves you with the column labels in row 1. So you use row 1 to name the columns then remove row 1.
df <- as.data.frame(datanew)
names(df) <- df[1, ]
df <- df[-1, ]
After which you propably want to cast each column to appropriate data types.
Upvotes: 1