Jingyu Gao
Jingyu Gao

Reputation: 71

Weird multiple columns in one column in R (fromJSON)

library(jsonlite) 
test <-as.dataframe(fromJSON('http://api.worldbank.org/v2/countries/all/indicators/SH.STA.ACSN?format=json')[2])
names(test)

However, it looks like there are more columns, such as the "country" column actually has "country.value" and "country.id" columns. This is annoying because "country.value" does not seem to exist, thus the following code returns errors saying this column is not in the data frame test. I only want to keep country.value. How can I resolve this? Does it have anything to do with how JSON returns data in R?

test$country.value

enter image description here

Upvotes: 2

Views: 233

Answers (1)

Dimitrios Zacharatos
Dimitrios Zacharatos

Reputation: 850

Does flatten work for you? I am not sure.

library(jsonlite) 
test <-data.frame(fromJSON('http://api.worldbank.org/v2/countries/all/indicators/SH.STA.ACSN?format=json')[2])
names(test)
test_flatten<-flatten(test)
names(test_flatten)

Upvotes: 0

Related Questions