R: Convert large list to data frame - r

I have a large list (of 10 elements) called res as shown below. Please, notice that I only show 3 of the elements so the post isn't too long.
> str(res)
List of 10
$ :'data.frame': 1 obs. of 13 variables:
..$ id : chr "121040004071"
..$ province : chr "Castellón/Castelló"
..$ comunidadAutonoma: chr "Comunitat Valenciana"
..$ muni : chr "Segorbe"
..$ type : chr "portal"
..$ address : chr "A-23"
..$ geom : chr "POINT(-0.428888910999945 39.806487449)"
..$ lat : num 39.8
..$ lng : num -0.429
..$ portalNumber : chr "23"
..$ stateMsg : chr "Resultado exacto de la búsqueda"
..$ state : chr "1"
..$ countryCode : chr "011"
$ :'data.frame': 1 obs. of 13 variables:
..$ id : chr "121040004071"
..$ province : chr "Castellón/Castelló"
..$ comunidadAutonoma: chr "Comunitat Valenciana"
..$ muni : chr "Segorbe"
..$ type : chr "portal"
..$ address : chr "A-23"
..$ geom : chr "POINT(-0.428888910999945 39.806487449)"
..$ lat : num 39.8
..$ lng : num -0.429
..$ portalNumber : chr "23"
..$ stateMsg : chr "Resultado exacto de la búsqueda"
..$ state : chr "1"
..$ countryCode : chr "011"
$ :'data.frame': 1 obs. of 13 variables:
..$ id : chr "121040004071"
..$ province : chr "Castellón/Castelló"
..$ comunidadAutonoma: chr "Comunitat Valenciana"
..$ muni : chr "Segorbe"
..$ type : chr "portal"
..$ address : chr "A-23"
..$ geom : chr "POINT(-0.428888910999945 39.806487449)"
..$ lat : num 39.8
..$ lng : num -0.429
..$ portalNumber : chr "23"
..$ stateMsg : chr "Resultado exacto de la búsqueda"
..$ state : chr "1"
..$ countryCode : chr "011"
Each observation corresponds to a certain address in the city of Valencia, Spain. After geocoding my 10 addresses, I ended up with 13 variables for each address containing information about longitude, latitude, province, etc.
I would like to make it a data frame so that for every row we have the main $:'data.frame and the rest of ..$ x are the variables/columns.
Thanks for your help

You can ouse the following functions:
Map function
list_data <- Map(as.data.frame, list_data)
rbindlist function
datarbind <- rbindlist(list_data)

Related

Insert elements into a list based on depth and `if` conditions using modify_depth and modify_if (purrr)

I'm learning some purrr commands, specifically the modify_* family of functions. I'm attemping to add price bins to items found in a grocery store (see below for my attempt and error code).
library(tidyverse)
Data
easybuy <- list(
"5520 N Division St, Spokane, WA 99208, USA",
list("bananas", "oranges"),
canned = list("olives", "fish", "jam"),
list("pork", "beef"),
list("hammer", "tape")
) %>%
map(list) %>%
# name the sublists
set_names(c("address",
"fruit",
"canned",
"meat",
"other")) %>%
# except for address, names the sublists "items"
modify_at(c(2:5), ~ set_names(.x, "items"))
Take a peek:
glimpse(easybuy)
#> List of 5
#> $ address:List of 1
#> ..$ : chr "5520 N Division St, Spokane, WA 99208, USA"
#> $ fruit :List of 1
#> ..$ items:List of 2
#> .. ..$ : chr "bananas"
#> .. ..$ : chr "oranges"
#> $ canned :List of 1
#> ..$ items:List of 3
#> .. ..$ : chr "olives"
#> .. ..$ : chr "fish"
#> .. ..$ : chr "jam"
#> $ meat :List of 1
#> ..$ items:List of 2
#> .. ..$ : chr "pork"
#> .. ..$ : chr "beef"
#> $ other :List of 1
#> ..$ items:List of 2
#> .. ..$ : chr "hammer"
#> .. ..$ : chr "tape"
My Attempt
Idea: go in a depth of two, and look for "items", append a "price". I'm not sure if I can nest the modify functions like this.
easybuy %>%
modify_depth(2, ~ modify_at(., "items", ~ append("price")))
#> Error: character indexing requires a named object
Desired
I would like the following structure (note the addition of "price" under each item):
List of 5
$ address:List of 1
..$ : chr "5520 N Division St, Spokane, WA 99208, USA"
$ fruit :List of 1
..$ items:List of 2
.. ..$ :List of 2
.. .. ..$ : chr "bananas"
.. .. ..$ : chr "price"
.. ..$ :List of 2
.. .. ..$ : chr "oranges"
.. .. ..$ : chr "price"
$ canned :List of 1
..$ items:List of 3
.. ..$ :List of 2
.. .. ..$ : chr "olives"
.. .. ..$ : chr "price"
.. ..$ :List of 2
.. .. ..$ : chr "fish"
.. .. ..$ : chr "price"
.. ..$ :List of 2
.. .. ..$ : chr "jam"
.. .. ..$ : chr "price"
$ meat :List of 1
..$ items:List of 2
.. ..$ :List of 2
.. .. ..$ : chr "pork"
.. .. ..$ : chr "price"
.. ..$ :List of 2
.. .. ..$ : chr "beef"
.. .. ..$ : chr "price"
$ other :List of 1
..$ items:List of 2
.. ..$ :List of 2
.. .. ..$ : chr "hammer"
.. .. ..$ : chr "price"
.. ..$ :List of 2
.. .. ..$ : chr "tape"
.. .. ..$ : chr "price"
This seems working. The map_if and function(x) !is.null(names(x)) make sure the change only happen if the name of the item is not NULL. ~modify_depth(.x, 2, function(y) list(y, "price")) creates the list you need.
library(tidyverse)
easybuy2 <- easybuy %>%
map_if(function(x) !is.null(names(x)),
~modify_depth(.x, 2, function(y) list(y, "price")))
Here is how the second item looks like.
easybuy2[[2]][[1]]
# [[1]]
# [[1]][[1]]
# [1] "bananas"
#
# [[1]][[2]]
# [1] "price"
#
#
# [[2]]
# [[2]][[1]]
# [1] "oranges"
#
# [[2]][[2]]
# [1] "price"
Or this also works.
easybuy3 <- easybuy %>%
modify_at(2:5, ~modify_depth(.x, 2, function(y) list(y, "price")))
identical(easybuy2, easybuy3)
# [1] TRUE
Update
easybuy4 <- easybuy %>%
map_if(function(x){
name <- names(x)
if(is.null(name)){
return(FALSE)
} else {
return(name %in% "items")
}
},
~modify_depth(.x, 2, function(y) list(y, "price")))
identical(easybuy2, easybuy4)
# [1] TRUE

How do I convert a JSON file to a data frame in R?

Link to data.
For my purposes, I downloaded the data from the above link and saved it as a JSON file.
json_convert <- do.call(rbind, lapply(paste(readLines("Myfile.json", warn=TRUE),
collapse=""),
jsonlite::fromJSON))
So far, I have managed to code the above. However, I am confused as to how I can convert this into a data frame. All help is appreciated.
Let's start by examining the data structure:
library(purrr)
library(tibble)
library(jsonlite)
my_json <- fromJSON("Myfile.json")
str(my_json)
List of 3
$ resource : chr "shotchartdetail"
$ parameters:List of 30
..$ LeagueID : chr "00"
..$ Season : chr "2017-18"
..$ SeasonType : chr "Regular Season"
..$ TeamID : int 1610612750
..$ PlayerID : int 0
..$ GameID : NULL
..$ Outcome : NULL
..$ Location : NULL
..$ Month : int 0
..$ SeasonSegment : NULL
..$ DateFrom : NULL
..$ DateTo : NULL
..$ OpponentTeamID: int 0
..$ VsConference : NULL
..$ VsDivision : NULL
..$ Position : NULL
..$ RookieYear : NULL
..$ GameSegment : NULL
..$ Period : int 0
..$ LastNGames : int 0
..$ ClutchTime : NULL
..$ AheadBehind : NULL
..$ PointDiff : NULL
..$ RangeType : int 0
..$ StartPeriod : int 1
..$ EndPeriod : int 10
..$ StartRange : int 0
..$ EndRange : int 28800
..$ ContextFilter : chr "SEASON_YEAR='2017-18'"
..$ ContextMeasure: chr "FGA"
$ resultSets:'data.frame': 2 obs. of 3 variables:
..$ name : chr [1:2] "Shot_Chart_Detail" "LeagueAverages"
..$ headers:List of 2
.. ..$ : chr [1:24] "GRID_TYPE" "GAME_ID" "GAME_EVENT_ID" "PLAYER_ID" ...
.. ..$ : chr [1:7] "GRID_TYPE" "SHOT_ZONE_BASIC" "SHOT_ZONE_AREA" "SHOT_ZONE_RANGE"
...
..$ rowSet :List of 2
.. ..$ : chr [1:7063, 1:24] "Shot Chart Detail" "Shot Chart Detail" "Shot Chart
Detail" "Shot Chart Detail" ...
.. ..$ : chr [1:20, 1:7] "League Averages" "League Averages" "League Averages" "League Averages" ...
Now you have to decide what it is that you want in your data frame.
I would assume that player statistics are in the first element of $rowSet (1:7063 = rows, 1:24 = columns) and the headers for those columns are in the first element of $resultSets$headers (1:24).
I'm sure there's a very elegant way to use the map functions in purrr. This isn't it, but it works:
my_list <- my_json %>%
flatten()
my_df <- my_list$rowSet[[1]] %>%
as.tibble() %>%
setNames(my_list$headers[[1]])
str(my_df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 7063 obs. of 24 variables:
$ GRID_TYPE : chr "Shot Chart Detail" "Shot Chart Detail" "Shot Chart Detail" "Shot Chart Detail" ...
$ GAME_ID : chr "0021700011" "0021700011" "0021700011" "0021700011" ...
$ GAME_EVENT_ID : chr "10" "12" "16" "21" ...
$ PLAYER_ID : chr "1626157" "202710" "202710" "201959" ...
$ PLAYER_NAME : chr "Karl-Anthony Towns" "Jimmy Butler" "Jimmy Butler" "Taj Gibson" ...
$ TEAM_ID : chr "1610612750" "1610612750" "1610612750" "1610612750" ...
$ TEAM_NAME : chr "Minnesota Timberwolves" "Minnesota Timberwolves" "Minnesota Timberwolves" "Minnesota Timberwolves" ...
$ PERIOD : chr "1" "1" "1" "1" ...
$ MINUTES_REMAINING : chr "11" "11" "10" "10" ...
$ SECONDS_REMAINING : chr "14" "9" "32" "21" ...
$ EVENT_TYPE : chr "Missed Shot" "Made Shot" "Missed Shot" "Missed Shot"
...
$ ACTION_TYPE : chr "Jump Shot" "Jump Shot" "Driving Reverse Layup Shot" "Jump Shot" ...
$ SHOT_TYPE : chr "2PT Field Goal" "3PT Field Goal" "2PT Field Goal" "3PT Field Goal" ...
$ SHOT_ZONE_BASIC : chr "Mid-Range" "Above the Break 3" "Restricted Area" "Left Corner 3" ...
$ SHOT_ZONE_AREA : chr "Left Side Center(LC)" "Right Side Center(RC)" "Center(C)" "Left Side(L)" ...
$ SHOT_ZONE_RANGE : chr "16-24 ft." "24+ ft." "Less Than 8 ft." "24+ ft." ...
$ SHOT_DISTANCE : chr "20" "25" "1" "22" ...
$ LOC_X : chr "-113" "199" "-11" "-225" ...
$ LOC_Y : chr "169" "152" "6" "16" ...
$ SHOT_ATTEMPTED_FLAG: chr "1" "1" "1" "1" ...
$ SHOT_MADE_FLAG : chr "0" "1" "0" "0" ...
$ GAME_DATE : chr "20171018" "20171018" "20171018" "20171018" ...
$ HTM : chr "SAS" "SAS" "SAS" "SAS" ...
$ VTM : chr "MIN" "MIN" "MIN" "MIN" ...

Expand nested dataframe into parent

I have a dataframe nested within a dataframe that I'm getting from Mongo. The number of rows match in each so that when viewed it looks like a typical dataframe. My question, how do I expand the nested dataframe into the parent so that I can run dplyr selects? See the layout below
'data.frame': 10 obs. of 2 variables:
$ _id : int 1551 1033 1061 1262 1032 1896 1080 1099 1679 1690
$ personalInfo:'data.frame': 10 obs. of 2 variables:
..$ FirstName :List of 10
.. ..$ : chr "Jack"
.. ..$ : chr "Yogesh"
.. ..$ : chr "Steven"
.. ..$ : chr "Richard"
.. ..$ : chr "Thomas"
.. ..$ : chr "Craig"
.. ..$ : chr "David"
.. ..$ : chr "Aman"
.. ..$ : chr "Frank"
.. ..$ : chr "Robert"
..$ MiddleName :List of 10
.. ..$ : chr "B"
.. ..$ : NULL
.. ..$ : chr "J"
.. ..$ : chr "I"
.. ..$ : chr "E"
.. ..$ : chr "A"
.. ..$ : chr "R"
.. ..$ : NULL
.. ..$ : chr "J"
.. ..$ : chr "E"
As per suggestion, here's how you recreate the data
id <- c(1551, 1033, 1061, 1262, 1032, 1896, 1080, 1099, 1679, 1690)
fname <- list("Jack","Yogesh","Steven","Richard","Thomas","Craig","David","Aman","Frank","Robert")
mname <- list("B",NULL,"J","I","E","A","R",NULL,"J","E")
sub <- as.data.frame(cbind(fname, mname))
master <- as.data.frame(id)
master$personalInfo <- sub
We could loop the 'personalInfo', change the NULL elements of the list to NA and convert it to a real dataset with 3 columns
library(tidyverse)
out <- master %>%
pull(personalInfo) %>%
map_df(~ map_chr(.x, ~ replace(.x, is.null(.x), NA))) %>%
bind_cols(master %>%
select(id), .)
str(out)
#'data.frame': 10 obs. of 3 variables:
# $ id : num 1551 1033 1061 1262 1032 ...
# $ fname: chr "Jack" "Yogesh" "Steven" "Richard" ...
# $ mname: chr "B" NA "J" "I" ...
While #akrun's answer is probably more practical and probably the way to tidy your data, I think this output is closer to what you describe.
I create a new environment where I put the data.frame's content, there I unlist to the said environment the content of your problematic column, and finally I wrap it all back into a data.frame.
I use a strange hack with cbind as as.data.frame is annoying with list columns. Using tibble::as_tibble works fine however.
new_env <- new.env()
list2env(master,new_env)
list2env(new_env$personalInfo,new_env)
rm(personalInfo,envir = new_env)
res <- as.data.frame(do.call(cbind,as.list(new_env))) # or as_tibble(as.list(new_env))
rm(new_env)
res
# fname id mname
# 1 Jack 1551 B
# 2 Yogesh 1033 NULL
# 3 Steven 1061 J
# 4 Richard 1262 I
# 5 Thomas 1032 E
# 6 Craig 1896 A
# 7 David 1080 R
# 8 Aman 1099 NULL
# 9 Frank 1679 J
# 10 Robert 1690 E
str(res)
# 'data.frame': 10 obs. of 3 variables:
# $ fname:List of 10
# ..$ : chr "Jack"
# ..$ : chr "Yogesh"
# ..$ : chr "Steven"
# ..$ : chr "Richard"
# ..$ : chr "Thomas"
# ..$ : chr "Craig"
# ..$ : chr "David"
# ..$ : chr "Aman"
# ..$ : chr "Frank"
# ..$ : chr "Robert"
# $ id :List of 10
# ..$ : num 1551
# ..$ : num 1033
# ..$ : num 1061
# ..$ : num 1262
# ..$ : num 1032
# ..$ : num 1896
# ..$ : num 1080
# ..$ : num 1099
# ..$ : num 1679
# ..$ : num 1690
# $ mname:List of 10
# ..$ : chr "B"
# ..$ : NULL
# ..$ : chr "J"
# ..$ : chr "I"
# ..$ : chr "E"
# ..$ : chr "A"
# ..$ : chr "R"
# ..$ : NULL
# ..$ : chr "J"
# ..$ : chr "E"

r: merge list of unnamed data sets

After importing data from a JSON stream, I have a data frame that is 621 lists of the same 22 variables.
List of 621
$ :List of 22
..$ _id : chr "55c79e711cbee48856a30886"
..$ number : num 1
..$ country : chr "Yemen"
..$ date : chr "2002-11-03T00:00:00.000Z"
..$ narrative : chr ""
..$ town : chr ""
..$ location : chr ""
..$ deaths : chr "6"
..$ deaths_min : chr "6"
..$ deaths_max : chr "6"
..$ civilians : chr "0"
..$ injuries : chr ""
..$ children : chr ""
..$ tweet_id : chr "278544689483890688"
..$ bureau_id : chr "YEM001"
..$ bij_summary_short: chr ""
..$ bij_link : chr ""
..$ target : chr ""
..$ lat : chr "15.47467"
..$ lon : chr "45.322755"
..$ articles : list()
..$ names : chr ""| __truncated__
$ :List of 22
..$ _id : chr "55c79e711cbee48856a30887"
..$ number : num 2
..$ country : chr "Pakistan"
..$ date : chr "2004-06-17T00:00:00.000Z"
..$ narrative : chr ""
..$ town : chr ""
..$ location : chr ""
..$ deaths : chr "6-8"
..$ deaths_min : chr "6"
..$ deaths_max : chr "8"
..$ civilians : chr "2"
..$ injuries : chr "1"
..$ children : chr "2"
..$ tweet_id : chr "278544750867533824"
..$ bureau_id : chr "B1"
..$ bij_summary_short: chr ""| __truncated__
..$ bij_link : chr ""
..$ target : chr ""
..$ lat : chr "32.30512565"
..$ lon : chr "69.57624435"
..$ articles : list()
..$ names : chr ""
...
How can I combine these lists into one data frame of 621 observations of 22 variables? Notice that all 621 lists are unnamed.
edit: Per request, here is how I got this data set:
library(rjson)
url <- 'http://api.dronestre.am/data'
document <- fromJSON(file=url, method='C')
str(document$strike)
Can you provide example on how you generated the data ? I did not test the answer but, the following should help. If you can update the Q, on how you came up with the data, I can work to try that.
update
library(rjson)
library(data.table)
library(dplyr)
url <- 'http://api.dronestre.am/data'
document <- fromJSON(file=url, method='C')
is(document)
listdata<- document$strike
df<-do.call(rbind,listdata) %>% as.data.table
dim(df)
purrr has a useful transpose function which 'inverts' a list. The $articles element causes trouble as it appears always to be empty, and scuppers you when you try to convert to a data.frame, so I've subsetted for it.
library(purrr)
df <- transpose(document$strike) %>%
t %>%
apply(FUN = unlist, MARGIN = 2)
df <- df[-21] %>% data.frame %>% tbl_df
df
Source: local data frame [621 x 21]
X_id number country date
(fctr) (dbl) (fctr) (fctr)
1 55c79e711cbee48856a30886 1 Yemen 2002-11-03T00:00:00.000Z
2 55c79e711cbee48856a30887 2 Pakistan 2004-06-17T00:00:00.000Z
3 55c79e711cbee48856a30888 3 Pakistan 2005-05-08T00:00:00.000Z
4 55c79e721cbee48856a30889 4 Pakistan 2005-11-05T00:00:00.000Z
5 55c79e721cbee48856a3088a 5 Pakistan 2005-12-01T00:00:00.000Z
6 55c79e721cbee48856a3088b 6 Pakistan 2006-01-06T00:00:00.000Z
7 55c79e721cbee48856a3088c 7 Pakistan 2006-01-13T00:00:00.000Z
8 55c79e721cbee48856a3088d 8 Pakistan 2006-10-30T00:00:00.000Z
9 55c79e721cbee48856a3088e 9 Pakistan 2007-01-16T00:00:00.000Z
10 55c79e721cbee48856a3088f 10 Pakistan 2007-04-27T00:00:00.000Z
.. ... ... ... ...
Variables not shown: narrative (fctr), town (fctr), location (fctr), deaths
(fctr), deaths_min (fctr), deaths_max (fctr), civilians (fctr), injuries
(fctr), children (fctr), tweet_id (fctr), bureau_id (fctr), bij_summary_short
(fctr), bij_link (fctr), target (fctr), lat (fctr), lon (fctr), names (fctr)

Why jsonlite parses data into a list object ?

I try to parse data from a web API with jsonlite but for some reason the object it returns is a list.
It is said in the jsonlite package documentation that simplification process will automatically convert JSON list into a more specific R class but in my case it doesn't work.
It is like simplifyVector, simplifyDataFrame and simplifyMatrix function are disabled but each one is enabled by default.
What I would like is a dataframe to retrieve the $Name data (EAC, EFL, ELC, etc.).
I also try with the rjson library but still the same problem.
Any idea what could be wrong ?
Thank you,
Please find the code I use :
raw <- getURL("https://www.cryptocompare.com/api/data/coinlist")
library(jsonlite)
data <- fromJSON(txt=raw)
> class(data)
[1] "list"
> typeof(data)
[1] "list"
> str(data)
[...]
..$ EAC :List of 13
.. ..$ Id : chr "4437"
.. ..$ Url : chr "/coins/eac/overview"
.. ..$ ImageUrl : chr "/media/19690/eac.png"
.. ..$ Name : chr "EAC"
.. ..$ CoinName : chr "EarthCoin"
.. ..$ FullName : chr "EarthCoin (EAC)"
.. ..$ Algorithm : chr "Scrypt"
.. ..$ ProofType : chr "PoW"
.. ..$ FullyPremined : chr "0"
.. ..$ TotalCoinSupply : chr "13500000000"
.. ..$ PreMinedValue : chr "N/A"
.. ..$ TotalCoinsFreeFloat: chr "N/A"
.. ..$ SortOrder : chr "100"
..$ EFL :List of 13
.. ..$ Id : chr "4438"
.. ..$ Url : chr "/coins/efl/overview"
.. ..$ ImageUrl : chr "/media/19692/efl.png"
.. ..$ Name : chr "EFL"
.. ..$ CoinName : chr "E-Gulden"
.. ..$ FullName : chr "E-Gulden (EFL)"
.. ..$ Algorithm : chr "Scrypt"
.. ..$ ProofType : chr "PoW"
.. ..$ FullyPremined : chr "0"
.. ..$ TotalCoinSupply : chr "21000000 "
.. ..$ PreMinedValue : chr "N/A"
.. ..$ TotalCoinsFreeFloat: chr "N/A"
.. ..$ SortOrder : chr "101"
..$ ELC :List of 13
.. ..$ Id : chr "4439"
.. ..$ Url : chr "/coins/elc/overview"
.. ..$ ImageUrl : chr "/media/19694/elc.png"
.. ..$ Name : chr "ELC"
.. ..$ CoinName : chr "Elacoin"
.. ..$ FullName : chr "Elacoin (ELC)"
.. ..$ Algorithm : chr "Scrypt"
.. ..$ ProofType : chr "PoW"
.. ..$ FullyPremined : chr "0"
.. ..$ TotalCoinSupply : chr "75000000"
.. ..$ PreMinedValue : chr "N/A"
.. ..$ TotalCoinsFreeFloat: chr "N/A"
.. ..$ SortOrder : chr "102"
.. [list output truncated]
$ Type : int 100
NULL
You showed the lower end of the structure, but the answer to the question regarding why a dataframe was not returned is seen at the top of the structure:
# note: needed `require(RCurl)` to obtain getURL
> str(data)
List of 6
$ Response : chr "Success"
$ Message : chr "Coin list succesfully returned!"
$ BaseImageUrl: chr "https://www.cryptocompare.com"
$ BaseLinkUrl : chr "https://www.cryptocompare.com"
$ Data :List of 492
..$ BTC :List of 13
.. ..$ Id : chr "1182"
.. ..$ Url : chr "/coins/btc/overview"
.. ..$ ImageUrl : chr "/media/19633/btc.png"
.. ..$ Name : chr "BTC"
.. ..$ CoinName : chr "Bitcoin"
.. ..$ FullName : chr "Bitcoin (BTC)"
.. ..$ Algorithm : chr "SHA256"
# ------snipped the many, many pages of output that followed---------
Furthermore the $Data node of that list has irregular lengths so coercing to a dataframe in one step might be difficult:
> table( sapply(data$Data, length))
12 13 14
2 478 12
After loading pkg:plyr which provides a useful function to rbind similar but not identical dataframes I'm able to contruct a useful starting point for furhter analysis:
require(plyr)
money <- do.call(rbind.fill, lapply( data$Data, data.frame, stringsAsFactors=FALSE))
str(money)
#------------
'data.frame': 492 obs. of 14 variables:
$ Id : chr "1182" "3808" "3807" "5038" ...
$ Url : chr "/coins/btc/overview" "/coins/ltc/overview" "/coins/dash/overview" "/coins/xmr/overview" ...
$ ImageUrl : chr "/media/19633/btc.png" "/media/19782/ltc.png" "/media/20626/dash.png" "/media/19969/xmr.png" ...
$ Name : chr "BTC" "LTC" "DASH" "XMR" ...
$ CoinName : chr "Bitcoin" "Litecoin" "DigitalCash" "Monero" ...
$ FullName : chr "Bitcoin (BTC)" "Litecoin (LTC)" "DigitalCash (DASH)" "Monero (XMR)" ...
$ Algorithm : chr "SHA256" "Scrypt" "X11" "CryptoNight" ...
$ ProofType : chr "PoW" "PoW" "PoW/PoS" "PoW" ...
$ FullyPremined : chr "0" "0" "0" "0" ...
$ TotalCoinSupply : chr "21000000" "84000000" "22000000" "0" ...
$ PreMinedValue : chr "N/A" "N/A" "N/A" "N/A" ...
$ TotalCoinsFreeFloat: chr "N/A" "N/A" "N/A" "N/A" ...
$ SortOrder : chr "1" "3" "4" "5" ...
$ TotalCoinsMined : chr NA NA NA NA ...
If you wanted to be able to access the rows by way of the abbreviations for those crypto-currencies, you could do:
rownames(money) <- names(data$Data)
Which now lets you do this:
> money[ "BTC", ]
Id Url ImageUrl Name CoinName
BTC 1182 /coins/btc/overview /media/19633/btc.png BTC Bitcoin
FullName Algorithm ProofType FullyPremined TotalCoinSupply
BTC Bitcoin (BTC) SHA256 PoW 0 21000000
PreMinedValue TotalCoinsFreeFloat SortOrder TotalCoinsMined
BTC N/A N/A 1 <NA>
Where before access would have been a bit more clunky:
> money[ money$Name=="BTC", ]
I reply to my own question as - already said in the comment section - returned object is already in it's simplest form. Probably that jsonlite cannot create data frame from multiple lists (lists imbrication).
The solution I have found is to use unlist and data.frame like this :
> df <- data.frame(unlist(data))
> class(df)
[1] "data.frame"

Resources