ProHoster > Blog > Tsamaiso > Ho holisa likholomo tse behiloeng - manane a sebelisa puo ea R (sephutheloana sa tidyr le mesebetsi e sa nepahalang ea lelapa)
Ho holisa likholomo tse behiloeng - manane a sebelisa puo ea R (sephutheloana sa tidyr le mesebetsi e sa nepahalang ea lelapa)
Maemong a mangata, ha u sebetsa ka karabo e fumanoang ho API, kapa ka boitsebiso leha e le bofe bo nang le mohaho o rarahaneng oa sefate, u tobane le liforomo tsa JSON le XML.
Liforomo tsena li na le melemo e mengata: li boloka data ka mokhoa o kopaneng 'me li u lumella ho qoba phetiso e sa hlokahaleng ea tlhahisoleseling.
Bobebe ba liforomo tsena ke ho rarahana ha tšebetso le tlhahlobo ea tsona. Lintlha tse sa hlophisoang li ke ke tsa sebelisoa lipalong 'me pono e ke ke ea hahoa holim'a eona.
Sengoliloeng sena ke tsoelopele e utloahalang ea khatiso "R package tidyr le mesebetsi ea eona e mecha ea pivot_longer le pivot_wider". E tla u thusa ho tlisa libopeho tsa data tse sa hlophisoang ka mokhoa o tloaelehileng le o loketseng bakeng sa tlhahlobo ea tabula o sebelisa sephutheloana tidyr, e kenyellelitsoeng khubung ea laebrari tidyverse, le lelapa la eona la mesebetsi unnest_*().
Tse ka hare
Haeba u thahasella tlhahlobo ea data, u ka 'na ua thahasella ea ka thelekramo и mang youtube dikanale. Boholo ba litaba bo nehetsoe puong ea R.
Rectangling(Tlhaloso ea mofetoleli, ha kea fumana likhetho tse lekaneng tsa phetolelo bakeng sa lentsoe lena, kahoo re tla e tlohela e le joalo.) ke ts'ebetso ea ho tlisa data e sa hlophisoang e nang le li-arrays tse behiloeng ka har'a tafole ea mahlakore a mabeli e nang le mela le litšiea tse tloaelehileng. IN tidyr Ho na le lits'ebetso tse 'maloa tse tla u thusa ho holisa litšiea tsa lethathamo le behiloeng le ho fokotsa data ho ea ka sebopeho sa tabular e nang le likhutlo li tharo:
unnest_longer() e nka karolo e 'ngoe le e' ngoe ea lethathamo la litšiea ebe e etsa mola o mocha.
unnest_wider() e nka karolo e 'ngoe le e 'ngoe ea lenane la likholomo ebe e theha kholomo e ncha.
unnest_auto() e ikhethela hore na ke ts'ebetso efe e molemo ho e sebelisa unnest_longer() kapa unnest_wider().
hoist() tšoana le unnest_wider() empa e khetha feela likarolo tse boletsoeng mme e u lumella ho sebetsa ka maemo a 'maloa a sehlaha.
Boholo ba mathata a amanang le ho tlisa data e sa hlophisoang ka maemo a 'maloa a sehlaha tafoleng ea mahlakore a mabeli a ka rarolloa ka ho kopanya mesebetsi e thathamisitsoeng le dplyr.
Ho bontša mekhoa ena, re tla sebelisa sephutheloana repurrrsive, e fanang ka manane a mangata a rarahaneng, a mekhahlelo e mengata a nkiloeng ho web API.
A re qale ka gh_users, lethathamo le nang le tlhahisoleseding e mabapi le basebelisi ba tšeletseng ba GitHub. Pele a re fetoleng lethathamo gh_users в tibble foreime:
users <- tibble( user = gh_users )
Sena se bonahala e le ntho e sa utloahaleng: hobaneng ho fana ka lethathamo gh_users, ho sebopeho sa data se rarahaneng haholoanyane? Empa foreimi ea data e na le molemo o moholo: e kopanya li-vector tse ngata e le hore ntho e 'ngoe le e' ngoe e lateloe nthong e le 'ngoe.
Karolo ka 'ngoe ea ntho users ke lenane le reheletsoeng leo element ka 'ngoe e emelang kholomo.
Tabeng ena, re na le tafole e nang le litšiea tse 30, 'me re ke ke ra hloka boholo ba tsona, kahoo re ka khona ho etsa joalo. unnest_wider() sebelisa hoist(). hoist() e re lumella ho ntša likarolo tse khethiloeng re sebelisa syntax e tšoanang le purrr::pluck():
users %>% hoist(user,
followers = "followers",
login = "login",
url = "html_url"
)
#> # A tibble: 6 x 4
#> followers login url user
#> <int> <chr> <chr> <list>
#> 1 303 gaborcsardi https://github.com/gaborcsardi <named list [27]>
#> 2 780 jennybc https://github.com/jennybc <named list [27]>
#> 3 3958 jtleek https://github.com/jtleek <named list [27]>
#> 4 115 juliasilge https://github.com/juliasilge <named list [27]>
#> 5 213 leeper https://github.com/leeper <named list [27]>
#> 6 34 masalmon https://github.com/masalmon <named list [27]>
hoist() e tlosa likarolo tse boletsoeng tse boletsoeng lethathamong la likholomo user want-meet.rukahoo u ka nahana hoist() joalo ka ho tsamaisa likarolo ho tloha lethathamong le ka hare la foreimi ea letsatsi ho ea boemong ba eona bo kaholimo.
Libaka tsa polokelo ea Github
Ho tsamaisana le lethathamo gh_repos re qala ka ho tšoana ka ho e fetolela ho tibble:
Lekhetlong lena likarolo user want-meet.ru emela lenane la polokelo ea mosebelisi enoa. Sebaka se seng le se seng sa polokelo ke pono e arohaneng, kahoo ho latela mohopolo oa data e makhethe (hoo e ka bang data e hloekisitsoeng) li lokela ho fetoha mela e mecha, ke ka lebaka leo re sebelisang unnest_longer() empa eseng unnest_wider():
repos <- repos %>% unnest_longer(repo)
repos
#> # A tibble: 176 x 1
#> repo
#> <list>
#> 1 <named list [68]>
#> 2 <named list [68]>
#> 3 <named list [68]>
#> 4 <named list [68]>
#> 5 <named list [68]>
#> 6 <named list [68]>
#> 7 <named list [68]>
#> 8 <named list [68]>
#> 9 <named list [68]>
#> 10 <named list [68]>
#> # … with 166 more rows
Hona joale re ka sebelisa unnest_wider() kapa hoist() :
repos %>% hoist(repo,
login = c("owner", "login"),
name = "name",
homepage = "homepage",
watchers = "watchers_count"
)
#> # A tibble: 176 x 5
#> login name homepage watchers repo
#> <chr> <chr> <chr> <int> <list>
#> 1 gaborcsardi after <NA> 5 <named list [65]>
#> 2 gaborcsardi argufy <NA> 19 <named list [65]>
#> 3 gaborcsardi ask <NA> 5 <named list [65]>
#> 4 gaborcsardi baseimports <NA> 0 <named list [65]>
#> 5 gaborcsardi citest <NA> 0 <named list [65]>
#> 6 gaborcsardi clisymbols "" 18 <named list [65]>
#> 7 gaborcsardi cmaker <NA> 0 <named list [65]>
#> 8 gaborcsardi cmark <NA> 0 <named list [65]>
#> 9 gaborcsardi conditions <NA> 0 <named list [65]>
#> 10 gaborcsardi crayon <NA> 52 <named list [65]>
#> # … with 166 more rows
Ela hloko tšebeliso c("owner", "login"): Sena se re lumella ho fumana boleng ba boemo ba bobeli ho tsoa lethathamong le behiloeng owner. Mokhoa o mong ke ho fumana lethathamo lohle owner ebe o sebedisa tshebetso unnest_wider() beha e 'ngoe le e' ngoe ea likarolo tsa eona ka kholomong:
Ho e-na le ho nahana ka ho khetha mosebetsi o nepahetseng unnest_longer() kapa unnest_wider() u ka sebelisa unnest_auto(). Ts'ebetso ena e sebelisa mekhoa e mengata ea heuristic ho khetha ts'ebetso e nepahetseng ka ho fetesisa ea ho fetola data, mme e bonts'a molaetsa mabapi le mokhoa o khethiloeng.
got_chars e na le sebopeho se ts'oanang ho gh_users: Ena ke lethathamo la manane a boletsoeng, moo karolo ka 'ngoe ea lenane le ka hare e hlalosang tšobotsi e itseng ea sebapali sa Game of Thrones. Ho tlisa got_chars Bakeng sa pono ea tafole, re qala ka ho theha foreimi ea letsatsi, joalo ka mehlaleng e fetileng, ebe re fetolela ntho ka 'ngoe hore e be kholomo e arohaneng:
chars <- tibble(char = got_chars)
chars
#> # A tibble: 30 x 1
#> char
#> <list>
#> 1 <named list [18]>
#> 2 <named list [18]>
#> 3 <named list [18]>
#> 4 <named list [18]>
#> 5 <named list [18]>
#> 6 <named list [18]>
#> 7 <named list [18]>
#> 8 <named list [18]>
#> 9 <named list [18]>
#> 10 <named list [18]>
#> # … with 20 more rows
chars2 <- chars %>% unnest_wider(char)
chars2
#> # A tibble: 30 x 18
#> url id name gender culture born died alive titles aliases father
#> <chr> <int> <chr> <chr> <chr> <chr> <chr> <lgl> <list> <list> <chr>
#> 1 http… 1022 Theo… Male Ironbo… In 2… "" TRUE <chr … <chr [… ""
#> 2 http… 1052 Tyri… Male "" In 2… "" TRUE <chr … <chr [… ""
#> 3 http… 1074 Vict… Male Ironbo… In 2… "" TRUE <chr … <chr [… ""
#> 4 http… 1109 Will Male "" "" In 2… FALSE <chr … <chr [… ""
#> 5 http… 1166 Areo… Male Norvos… In 2… "" TRUE <chr … <chr [… ""
#> 6 http… 1267 Chett Male "" At H… In 2… FALSE <chr … <chr [… ""
#> 7 http… 1295 Cres… Male "" In 2… In 2… FALSE <chr … <chr [… ""
#> 8 http… 130 Aria… Female Dornish In 2… "" TRUE <chr … <chr [… ""
#> 9 http… 1303 Daen… Female Valyri… In 2… "" TRUE <chr … <chr [… ""
#> 10 http… 1319 Davo… Male Wester… In 2… "" TRUE <chr … <chr [… ""
#> # … with 20 more rows, and 7 more variables: mother <chr>, spouse <chr>,
#> # allegiances <list>, books <list>, povBooks <list>, tvSeries <list>,
#> # playedBy <list>
sebopeho sa got_chars batla thata hofeta gh_users, hobane likarolo tse ling tsa lethathamo char ka bobona ke lethathamo, ka lebaka leo re fumana litšiea - manane:
Liketso tsa hau tse eketsehileng li itšetlehile ka lipakane tsa tlhahlobo. Mohlomong o hloka ho beha leseli meleng ea buka e 'ngoe le e 'ngoe le letoto leo sebapali se hlahang ho lona:
chars2 %>%
select(name, books, tvSeries) %>%
pivot_longer(c(books, tvSeries), names_to = "media", values_to = "value") %>%
unnest_longer(value)
#> # A tibble: 180 x 3
#> name media value
#> <chr> <chr> <chr>
#> 1 Theon Greyjoy books A Game of Thrones
#> 2 Theon Greyjoy books A Storm of Swords
#> 3 Theon Greyjoy books A Feast for Crows
#> 4 Theon Greyjoy tvSeries Season 1
#> 5 Theon Greyjoy tvSeries Season 2
#> 6 Theon Greyjoy tvSeries Season 3
#> 7 Theon Greyjoy tvSeries Season 4
#> 8 Theon Greyjoy tvSeries Season 5
#> 9 Theon Greyjoy tvSeries Season 6
#> 10 Tyrion Lannister books A Feast for Crows
#> # … with 170 more rows
Kapa mohlomong u batla ho etsa tafole e u lumellang ho ts'oana le sebopeho le mosebetsi:
chars2 %>%
select(name, title = titles) %>%
unnest_longer(title)
#> # A tibble: 60 x 2
#> name title
#> <chr> <chr>
#> 1 Theon Greyjoy Prince of Winterfell
#> 2 Theon Greyjoy Captain of Sea Bitch
#> 3 Theon Greyjoy Lord of the Iron Islands (by law of the green lands)
#> 4 Tyrion Lannister Acting Hand of the King (former)
#> 5 Tyrion Lannister Master of Coin (former)
#> 6 Victarion Greyjoy Lord Captain of the Iron Fleet
#> 7 Victarion Greyjoy Master of the Iron Victory
#> 8 Will ""
#> 9 Areo Hotah Captain of the Guard at Sunspear
#> 10 Chett ""
#> # … with 50 more rows
(Ela hloko litekanyetso tse se nang letho "" tšimong title, sena se bakoa ke liphoso tse entsoeng ha u kenya data ho got_chars: ha e le hantle, litlhaku tseo ho tsona ho se nang buka e tsamaellanang le lihlooho tsa letoto la TV lebaleng title e tlameha ho ba le vector ea bolelele ba 0, eseng vector ea bolelele ba 1 e nang le khoele e se nang letho.)
Re ka ngola mohlala o ka holimo re sebelisa ts'ebetso unnest_auto(). Mokhoa ona o loketse tlhahlobo ea nako e le 'ngoe, empa ha ua lokela ho itšetleha ka eona unnest_auto() bakeng sa tshebediso kgafetsa. Taba ke hore haeba sebopeho sa hau sa data se fetoha unnest_auto() e ka fetola mokhoa o khethiloeng oa ho fetola data haeba qalong e ne e atolositse likholomo tsa lenane ka mela e sebelisa unnest_longer(), joale ha sebopeho sa data e kenang e fetoha, logic e ka fetoloa molemong unnest_wider(), 'me ho sebelisa mokhoa ona kamehla ho ka lebisa liphoso tse sa lebelloang.
tibble(char = got_chars) %>%
unnest_auto(char) %>%
select(name, title = titles) %>%
unnest_auto(title)
#> Using `unnest_wider(char)`; elements have 18 names in common
#> Using `unnest_longer(title)`; no element has names
#> # A tibble: 60 x 2
#> name title
#> <chr> <chr>
#> 1 Theon Greyjoy Prince of Winterfell
#> 2 Theon Greyjoy Captain of Sea Bitch
#> 3 Theon Greyjoy Lord of the Iron Islands (by law of the green lands)
#> 4 Tyrion Lannister Acting Hand of the King (former)
#> 5 Tyrion Lannister Master of Coin (former)
#> 6 Victarion Greyjoy Lord Captain of the Iron Fleet
#> 7 Victarion Greyjoy Master of the Iron Victory
#> 8 Will ""
#> 9 Areo Hotah Captain of the Guard at Sunspear
#> 10 Chett ""
#> # … with 50 more rows
Geocoding ka Google
Ka mor'a moo, re tla sheba sebopeho se rarahaneng haholoanyane sa data e fumanoeng ho tsoa ho ts'ebeletso ea geocoding ea Google. Lintlha tsa caching li khahlanong le melao ea ho sebetsa le API ea limmapa tsa Google, kahoo ke tla qala ka ho ngola sephutheloana se bonolo ho potoloha API. E ipapisitseng le ho boloka senotlolo sa Google Maps API sebakeng se fapaneng sa tikoloho; Haeba ha u na senotlolo sa ho sebetsa le Google Maps API e bolokiloeng tikolohong ea hau, likaroloana tsa khoutu tse hlahisitsoeng karolong ena li ke ke tsa sebelisoa.
has_key <- !identical(Sys.getenv("GOOGLE_MAPS_API_KEY"), "")
if (!has_key) {
message("No Google Maps API key found; code chunks will not be run")
}
# https://developers.google.com/maps/documentation/geocoding
geocode <- function(address, api_key = Sys.getenv("GOOGLE_MAPS_API_KEY")) {
url <- "https://maps.googleapis.com/maps/api/geocode/json"
url <- paste0(url, "?address=", URLencode(address), "&key=", api_key)
jsonlite::read_json(url)
}
Lethathamo leo ts'ebetso ena e khutlang le rarahane haholo:
Ka lehlohonolo, re ka rarolla bothata ba ho fetolela data ena ho foromo ea tabular mohato ka mohato ho sebelisa mesebetsi tidyr. Ho etsa hore mosebetsi o be thata le ho feta, ke tla qala ka ho etsa litoropo tse 'maloa:
city <- c ( "Houston" , "LA" , "New York" , "Chicago" , "Springfield" ) city_geo <- purrr::map (city, geocode)
Ke tla fetolela sephetho sa sephetho ho tibble, bakeng sa boiketlo, ke tla eketsa kholomo e nang le lebitso la toropo le lumellanang.
loc <- tibble(city = city, json = city_geo)
loc
#> # A tibble: 5 x 2
#> city json
#> <chr> <list>
#> 1 Houston <named list [2]>
#> 2 LA <named list [2]>
#> 3 New York <named list [2]>
#> 4 Chicago <named list [2]>
#> 5 Springfield <named list [2]>
Boemo ba pele bo na le likarolo status и result, eo re ka atolosang ka eona unnest_wider() :
loc %>%
unnest_wider(json)
#> # A tibble: 5 x 3
#> city results status
#> <chr> <list> <chr>
#> 1 Houston <list [1]> OK
#> 2 LA <list [1]> OK
#> 3 New York <list [1]> OK
#> 4 Chicago <list [1]> OK
#> 5 Springfield <list [1]> OK
lemoha hore results ke lenane la maemo a mangata. Litoropo tse ngata li na le karolo e le 'ngoe (e emelang boleng bo ikhethang bo tsamaellanang le geocoding API), empa Springfield e na le tse peli. Re ka li hula ka mela e arohaneng le unnest_longer() :
loc %>%
unnest_wider(json) %>%
unnest_longer(results)
#> # A tibble: 5 x 3
#> city results status
#> <chr> <list> <chr>
#> 1 Houston <named list [5]> OK
#> 2 LA <named list [5]> OK
#> 3 New York <named list [5]> OK
#> 4 Chicago <named list [5]> OK
#> 5 Springfield <named list [5]> OK
Hona joale kaofela li na le likarolo tse tšoanang, tse ka netefatsoang li sebelisoa unnest_wider():
loc %>%
unnest_wider(json) %>%
unnest_longer(results) %>%
unnest_wider(results)
#> # A tibble: 5 x 7
#> city address_componen… formatted_addre… geometry place_id types status
#> <chr> <list> <chr> <list> <chr> <lis> <chr>
#> 1 Houst… <list [4]> Houston, TX, USA <named … ChIJAYWN… <lis… OK
#> 2 LA <list [4]> Los Angeles, CA… <named … ChIJE9on… <lis… OK
#> 3 New Y… <list [3]> New York, NY, U… <named … ChIJOwg_… <lis… OK
#> 4 Chica… <list [4]> Chicago, IL, USA <named … ChIJ7cv0… <lis… OK
#> 5 Sprin… <list [5]> Springfield, MO… <named … ChIJP5jI… <lis… OK
Re ka fumana likhokahano tsa latitude le longitude tsa toropo ka 'ngoe ka ho holisa lenane geometry:
loc %>%
unnest_wider(json) %>%
unnest_longer(results) %>%
unnest_wider(results) %>%
unnest_wider(geometry)
#> # A tibble: 5 x 10
#> city address_compone… formatted_addre… bounds location location_type
#> <chr> <list> <chr> <list> <list> <chr>
#> 1 Hous… <list [4]> Houston, TX, USA <name… <named … APPROXIMATE
#> 2 LA <list [4]> Los Angeles, CA… <name… <named … APPROXIMATE
#> 3 New … <list [3]> New York, NY, U… <name… <named … APPROXIMATE
#> 4 Chic… <list [4]> Chicago, IL, USA <name… <named … APPROXIMATE
#> 5 Spri… <list [5]> Springfield, MO… <name… <named … APPROXIMATE
#> # … with 4 more variables: viewport <list>, place_id <chr>, types <list>,
#> # status <chr>
'Me joale sebaka seo u lokelang ho se atolosa location:
loc %>%
unnest_wider(json) %>%
unnest_longer(results) %>%
unnest_wider(results) %>%
unnest_wider(geometry) %>%
unnest_wider(location)
#> # A tibble: 5 x 11
#> city address_compone… formatted_addre… bounds lat lng location_type
#> <chr> <list> <chr> <list> <dbl> <dbl> <chr>
#> 1 Hous… <list [4]> Houston, TX, USA <name… 29.8 -95.4 APPROXIMATE
#> 2 LA <list [4]> Los Angeles, CA… <name… 34.1 -118. APPROXIMATE
#> 3 New … <list [3]> New York, NY, U… <name… 40.7 -74.0 APPROXIMATE
#> 4 Chic… <list [4]> Chicago, IL, USA <name… 41.9 -87.6 APPROXIMATE
#> 5 Spri… <list [5]> Springfield, MO… <name… 37.2 -93.3 APPROXIMATE
#> # … with 4 more variables: viewport <list>, place_id <chr>, types <list>,
#> # status <chr>
Leha ho le joalo, unnest_auto() e nolofatsa ts'ebetso e hlalositsoeng ka likotsi tse ling tse ka bakoang ke ho fetola sebopeho sa data e kenang:
loc %>%
unnest_auto(json) %>%
unnest_auto(results) %>%
unnest_auto(results) %>%
unnest_auto(geometry) %>%
unnest_auto(location)
#> Using `unnest_wider(json)`; elements have 2 names in common
#> Using `unnest_longer(results)`; no element has names
#> Using `unnest_wider(results)`; elements have 5 names in common
#> Using `unnest_wider(geometry)`; elements have 4 names in common
#> Using `unnest_wider(location)`; elements have 2 names in common
#> # A tibble: 5 x 11
#> city address_compone… formatted_addre… bounds lat lng location_type
#> <chr> <list> <chr> <list> <dbl> <dbl> <chr>
#> 1 Hous… <list [4]> Houston, TX, USA <name… 29.8 -95.4 APPROXIMATE
#> 2 LA <list [4]> Los Angeles, CA… <name… 34.1 -118. APPROXIMATE
#> 3 New … <list [3]> New York, NY, U… <name… 40.7 -74.0 APPROXIMATE
#> 4 Chic… <list [4]> Chicago, IL, USA <name… 41.9 -87.6 APPROXIMATE
#> 5 Spri… <list [5]> Springfield, MO… <name… 37.2 -93.3 APPROXIMATE
#> # … with 4 more variables: viewport <list>, place_id <chr>, types <list>,
#> # status <chr>
Hape re ka sheba aterese ea pele ea toropo e 'ngoe le e 'ngoe:
loc %>%
unnest_wider(json) %>%
hoist(results, first_result = 1) %>%
unnest_wider(first_result) %>%
unnest_wider(geometry) %>%
unnest_wider(location)
#> # A tibble: 5 x 11
#> city address_compone… formatted_addre… bounds lat lng location_type
#> <chr> <list> <chr> <list> <dbl> <dbl> <chr>
#> 1 Hous… <list [4]> Houston, TX, USA <name… 29.8 -95.4 APPROXIMATE
#> 2 LA <list [4]> Los Angeles, CA… <name… 34.1 -118. APPROXIMATE
#> 3 New … <list [3]> New York, NY, U… <name… 40.7 -74.0 APPROXIMATE
#> 4 Chic… <list [4]> Chicago, IL, USA <name… 41.9 -87.6 APPROXIMATE
#> 5 Spri… <list [5]> Springfield, MO… <name… 37.2 -93.3 APPROXIMATE
#> # … with 4 more variables: viewport <list>, place_id <chr>, types <list>,
#> # status <chr>
Kapa sebelisa hoist() bakeng sa ho qoela ka mekhahlelo e mengata ho ea ka kotloloho ho lat и lng.
loc %>%
hoist(json,
lat = list("results", 1, "geometry", "location", "lat"),
lng = list("results", 1, "geometry", "location", "lng")
)
#> # A tibble: 5 x 4
#> city lat lng json
#> <chr> <dbl> <dbl> <list>
#> 1 Houston 29.8 -95.4 <named list [2]>
#> 2 LA 34.1 -118. <named list [2]>
#> 3 New York 40.7 -74.0 <named list [2]>
#> 4 Chicago 41.9 -87.6 <named list [2]>
#> 5 Springfield 37.2 -93.3 <named list [2]>
Tlhaloso ea Sharla Gelfand
Qetellong, re tla sheba sebopeho se rarahaneng ka ho fetisisa - discography ea Sharla Gelfand. Joalo ka mehlala e kaholimo, re qala ka ho fetolela lenane ho foreimi ea data ea kholumo e le 'ngoe, ebe re e atolosa e le hore karolo ka 'ngoe e be kholomo e arohaneng. Hape ke fetola mohala date_added ho latela mokhoa o nepahetseng oa letsatsi le nako ho R.
discs <- tibble(disc = discog) %>%
unnest_wider(disc) %>%
mutate(date_added = as.POSIXct(strptime(date_added, "%Y-%m-%dT%H:%M:%S")))
discs
#> # A tibble: 155 x 5
#> instance_id date_added basic_information id rating
#> <int> <dttm> <list> <int> <int>
#> 1 354823933 2019-02-16 17:48:59 <named list [11]> 7496378 0
#> 2 354092601 2019-02-13 14:13:11 <named list [11]> 4490852 0
#> 3 354091476 2019-02-13 14:07:23 <named list [11]> 9827276 0
#> 4 351244906 2019-02-02 11:39:58 <named list [11]> 9769203 0
#> 5 351244801 2019-02-02 11:39:37 <named list [11]> 7237138 0
#> 6 351052065 2019-02-01 20:40:53 <named list [11]> 13117042 0
#> 7 350315345 2019-01-29 15:48:37 <named list [11]> 7113575 0
#> 8 350315103 2019-01-29 15:47:22 <named list [11]> 10540713 0
#> 9 350314507 2019-01-29 15:44:08 <named list [11]> 11260950 0
#> 10 350314047 2019-01-29 15:41:35 <named list [11]> 11726853 0
#> # … with 145 more rows
Boemong bona, re fumana leseli mabapi le hore na disc ka 'ngoe e kentsoe neng ho discography ea Sharla, empa ha re bone data efe kapa efe ka li-disc tseo. Ho etsa sena, re hloka ho eketsa kholomo basic_information:
discs %>% unnest_wider(basic_information)
#> Column name `id` must not be duplicated.
#> Use .name_repair to specify repair.
Ka bomalimabe, re tla fumana phoso, hobane... ka hare ho lenane basic_information ho na le kholomo ea lebitso le tšoanang basic_information. Haeba phoso e joalo e etsahala, e le hore u tsebe hantle sesosa sa eona, u ka e sebelisa names_repair = "unique":
Joale u ka li kopanya hape ho dataset ea mantlha ha ho hlokahala.
fihlela qeto e
Ho fihla khubung ea laebrari tidyverse e kenyelletsa liphutheloana tse ngata tse sebetsang tse kopantsoeng ke filosofi e tloaelehileng ea ts'ebetso ea data.
Sehloohong sena re hlahlobile lelapa la mesebetsi unnest_*(), tse reretsweng ho sebetsa ka ho ntsha dintho ho tswa mananeng a dihlaha. Sephutheloana sena se na le likarolo tse ling tse ngata tse molemo tse etsang hore ho be bonolo ho fetolela data ho latela mohopolo Lintlha tse Hloekileng.