tidyr
本文介紹 R 的套組包 tidyr 的主要功能,我們首先安裝並調接該套組包以備後續使用。
install.packages("tidyr")
library("tidyr")
該套組包的主要功能係將數據給弄「整齊」的,所謂的整齊的數據(Tidy Data),在表格式的數據集之中,應滿足以下三個條件:
- 數據集中的每一變項,應在其所屬直欄;
- 數據集中的每一觀測例,應在其所屬橫排;
- 數據集中的每一數據值,應在其所屬儲存格。
以 tidyr 套組包所帶的數據框 table1 為例,即符合整齊數據集的要求,其中:1.各直欄分別為國家(文字型別)、年度(整數型別)、個案(整數型別)、人口(整數型別),共四個變項;2.各橫排分別為不同的觀測例(此處為六例);3.各數據值分別置於適當的儲存格處。
> table1
Source: local data frame [6 x 4]
country year cases population
(chr) (int) (int) (int)
1 Afghanistan 1999 745 19987071
2 Afghanistan 2000 2666 20595360
3 Brazil 1999 37737 172006362
4 Brazil 2000 80488 174504898
5 China 1999 212258 1272915272
6 China 2000 213766 1280428583
gather()(聚集)
在此演示例,須先安裝並調接 lubricate 套組包,用於處理日期時間數據。
install.packages("lubricate")
library("lubricate")
此一gather()
函式係將寬式數據框轉為長式數據框。先創設一個寬式的樣本數據框 df,再利用該函式將其轉為長式數據框,第一參數為待處理的數據框(本例為 df),第二參數為「鍵」,係以待聚集的各直欄名稱為值的直欄名稱(本例為「區域」),第三參數為「值」,係指待聚集的各該欄內元素在聚集後的直欄名稱(本例為「業績」),第四開始的參數為待聚集的各直欄名稱(本例為「台北、台中、高雄」)。
> set.seed(1)
> df <- data.frame(
+ 日期 = as.Date("2016-01-31") %m+% months(0:5),
+ 台北 = round(rnorm(6, 120, 6)),
+ 台中 = round(rnorm(6, 80, 6)),
+ 高雄 = round(rnorm(6, 100, 6)))
> df
日期 台北 台中 高雄
1 2016-01-31 116 83 96
2 2016-02-29 121 84 87
3 2016-03-31 115 83 107
4 2016-04-30 130 78 100
5 2016-05-31 122 89 100
6 2016-06-30 115 82 106
>
> df_1 <- gather(data = df, key = 區域, value = 業績, 台北:高雄)
> # 在此第四個起算的引數「台北, 台中, 高雄」可用「台北:高雄」代表。
> df_1
日期 區域 業績
1 2016-01-31 台北 116
2 2016-02-29 台北 121
3 2016-03-31 台北 115
4 2016-04-30 台北 130
5 2016-05-31 台北 122
6 2016-06-30 台北 115
7 2016-01-31 台中 83
8 2016-02-29 台中 84
9 2016-03-31 台中 83
10 2016-04-30 台中 78
11 2016-05-31 台中 89
12 2016-06-30 台中 82
13 2016-01-31 高雄 96
14 2016-02-29 高雄 87
15 2016-03-31 高雄 107
16 2016-04-30 高雄 100
17 2016-05-31 高雄 100
18 2016-06-30 高雄 106
>
> # 以下陳述之第四位位起的引數「台北, 台中, 高雄」亦得以「-日期」取代,
> # 表示 df 數據框中除了「日期」直欄外,其他全為待處理的,其回傳結果同上,不再贅表。
> gather(df, 區域, 業績, -日期)
以下用 tidyr 自帶的非整齊數據集 table4a(個案)、table4b(人口)為例說明:
> table4a # 個案數據集。
Source: local data frame [3 x 3]
country 1999 2000
(chr) (int) (int)
1 Afghanistan 745 2666
2 Brazil 37737 80488
3 China 212258 213766
> table4b # 人口數據集。
Source: local data frame [3 x 3]
country 1999 2000
(chr) (int) (int)
1 Afghanistan 19987071 20595360
2 Brazil 172006362 174504898
3 China 1272915272 1280428583
我們分別對 table4a 與 table4b 適用gather()
函式,兩者的「鍵」皆設為 "year",而由於前者的數值為個案,後者的數值為人口,故在「值」參數各自以 "cases" 與 "population" 為引數。
> table4a <- gather(table4a, "year", "cases", 2:3)
> table4a
Source: local data frame [6 x 3]
country year cases
(chr) (chr) (int)
1 Afghanistan 1999 745
2 Brazil 1999 37737
3 China 1999 212258
4 Afghanistan 2000 2666
5 Brazil 2000 80488
6 China 2000 213766
>
> table4b <- gather(table4b, "year", "population", 2:3)
> table4b
Source: local data frame [6 x 3]
country year population
(chr) (chr) (int)
1 Afghanistan 1999 19987071
2 Brazil 1999 172006362
3 China 1999 1272915272
4 Afghanistan 2000 20595360
5 Brazil 2000 174504898
6 China 2000 1280428583
最後再用 dplyr 套組包的inner_join()
函式將以上回傳的二數據框給連接起來,即能成為如 table1 之整齊數據框。
> library("dplyr")
> inner_join(table4a, table4b)
Joining by: c("country", "year")
Source: local data frame [6 x 4]
country year cases population
(chr) (chr) (int) (int)
1 Afghanistan 1999 745 19987071
2 Brazil 1999 37737 172006362
3 China 1999 212258 1272915272
4 Afghanistan 2000 2666 20595360
5 Brazil 2000 80488 174504898
6 China 2000 213766 1280428583
spread()(分散)
此一函式係將長式數據框轉為寬式數據框。spread()
的作用恰好與前述的gather()
相反,係將待處理的數據框中的「鍵值對」分散至多個直欄,本例將前述長式的 df_1 的「鍵」欄與「值」欄分散成多欄。回傳結果的直欄排列方式為「台中、台北、高雄」而非處理前的「台北、台中、高雄」,此係因依內碼排列。
> spread(data = df_1, key = 區域, value = 業績)
日期 台中 台北 高雄
1 2016-01-31 83 116 96
2 2016-02-29 84 121 87
3 2016-03-31 83 115 107
4 2016-04-30 78 130 100
5 2016-05-31 89 122 100
6 2016-06-30 82 115 106
缺漏值分為顯式缺漏值(explicit missing value)與隱式缺漏值(implicit missing value),前者為標示NA
可供識別,後者為橫排的不存在,不論寬式數據框存在何種缺漏值,皆得以spread()
的 fill 參數予以指定特定引數取代該缺漏值,該參數以 NA 為默認引數。以下先將 df_1 的 (2, 1), (6, 3), (10, 3), (17, 3) 這 4 個元素設為顯示缺漏值 NA,接著將最末橫排刪除為隱式缺漏值。
> idx <- matrix(data = c(2, 1, 6, 3, 10, 3, 17, 3), nrow = 4, byrow = TRUE)
> # 創設索引矩陣。
> idx
[,1] [,2]
[1,] 2 1
[2,] 6 3
[3,] 10 3
[4,] 17 3
>
> df_1[idx] <- NA # 將長式數據框 df_1 被 dix 矩陣所索引的元素之值,以缺漏值替代。
> df_1
日期 區域 業績
1 2016-01-31 台北 116
2 <NA> 台北 121 # (2, 1) 位置為顯示缺漏值。
3 2016-03-31 台北 115
4 2016-04-30 台北 130
5 2016-05-31 台北 122
6 2016-06-30 台北 NA # (6, 3) 位置為顯示缺漏值。
7 2016-01-31 台中 83
8 2016-02-29 台中 84
9 2016-03-31 台中 83
10 2016-04-30 台中 NA # (10, 3) 位置為顯示缺漏值。
11 2016-05-31 台中 89
12 2016-06-30 台中 82
13 2016-01-31 高雄 96
14 2016-02-29 高雄 87
15 2016-03-31 高雄 107
16 2016-04-30 高雄 100
17 2016-05-31 高雄 NA # (17, 3) 位置為顯示缺漏值。
18 2016-06-30 高雄 106
>
> df_1 <- df_1[-nrow(df_1), ] # 移除最末橫排,創造隱式缺漏值。
> df_1
日期 區域 業績
1 2016-01-31 台北 116
2 <NA> 台北 121
3 2016-03-31 台北 115
4 2016-04-30 台北 130
5 2016-05-31 台北 122
6 2016-06-30 台北 NA
7 2016-01-31 台中 83
8 2016-02-29 台中 84
9 2016-03-31 台中 83
10 2016-04-30 台中 NA
11 2016-05-31 台中 89
12 2016-06-30 台中 82
13 2016-01-31 高雄 96
14 2016-02-29 高雄 87
15 2016-03-31 高雄 107
16 2016-04-30 高雄 100
17 2016-05-31 高雄 NA
在 df_1 有多處缺漏值的情形下,先後執行spread()
共二次,第一次不指定 fill 參數,第二次指定 fill 之引數值為 0,結果分別如下,而由於長式數據框缺少一個日期數據,故改為寬式格式時會增加一橫排(因為找不到對應位置):
> spread(data = df_1, key = 區域, value = 業績)
日期 台中 台北 高雄
1 2016-01-31 83 116 96
2 2016-02-29 84 NA 87
3 2016-03-31 83 115 107
4 2016-04-30 NA 130 100
5 2016-05-31 89 122 NA
6 2016-06-30 82 NA NA
7 <NA> NA 121 NA
> spread(data = df_1, key = 區域, value = 業績, fill = 0)
日期 台中 台北 高雄
1 2016-01-31 83 116 96
2 2016-02-29 84 0 87
3 2016-03-31 83 115 107
4 2016-04-30 0 130 100
5 2016-05-31 89 122 0
6 2016-06-30 82 0 0
7 <NA> 0 121 0
以下用 tidyr 自帶的非整齊數據集 table2 為例說明:
> table2
Source: local data frame [12 x 4]
country year type count
(chr) (int) (chr) (int)
1 Afghanistan 1999 cases 745
2 Afghanistan 1999 population 19987071
3 Afghanistan 2000 cases 2666
4 Afghanistan 2000 population 20595360
5 Brazil 1999 cases 37737
6 Brazil 1999 population 172006362
7 Brazil 2000 cases 80488
8 Brazil 2000 population 174504898
9 China 1999 cases 212258
10 China 1999 population 1272915272
11 China 2000 cases 213766
12 China 2000 population 1280428583
在此要將 table2 的「鍵」欄與「值」欄分散,前者為 type,後者為 count,回傳結果如 table1 之整齊數據框。
> spread(data = table2, key = type, value = count)
Source: local data frame [6 x 4]
country year cases population
(chr) (int) (int) (int)
1 Afghanistan 1999 745 19987071
2 Afghanistan 2000 2666 20595360
3 Brazil 1999 37737 172006362
4 Brazil 2000 80488 174504898
5 China 1999 212258 1272915272
6 China 2000 213766 1280428583
separate()(分隔)
將特定直欄分散至多個直欄,本例將數據框 df 的日期直欄,分散為年、月、日共三直欄,演示如下:
> df_2 <- separate(data = df, col = 日期, into = c("年", "月", "日"))
> df_2
年 月 日 台北 台中 高雄
1 2016 01 31 116 83 96
2 2016 02 29 121 84 87
3 2016 03 31 115 83 107
4 2016 04 30 130 78 100
5 2016 05 31 122 89 100
6 2016 06 30 115 82 106
separate_rows()(分隔橫排)
如一數據框中有一個以上的直欄其元素含有不等數目的值,本函式可將該欄具多重數據之內容,分隔至不同橫排。以下創設一個數據框,包括:地區、姓名、年次,共三直欄,字串不轉為因子,其中姓名、年次這二直欄,具有多重數據。
> sr <- data.frame(
+ 區域 = c("台北", "台中", "高雄"),
+ 姓名 = c("趙一, 錢二, 孫三, 李四", "周五, 吳六, 鄭七", "馮九, 陳十"),
+ 年次 = c("1981, 1982, 1983, 1984", "1985, 1986, 1987", "1989, 1990"),
+ stringsAsFactors = FALSE)
> sr
區域 姓名 年次
1 台北 趙一, 錢二, 孫三, 李四 1981, 1982, 1983, 1984
2 台中 周五, 吳六, 鄭七 1985, 1986, 1987
3 高雄 馮九, 陳十 1989, 1990
將所創設的數據框,對其具多重數據的直欄內容,分隔至不同橫排,演示如下:
> separate_rows(sr, 姓名, 年次)
Source: local data frame [9 x 3]
區域 姓名 年次
(chr) (chr) (chr)
1 台北 趙一 1981
2 台北 錢二 1982
3 台北 孫三 1983
4 台北 李四 1984
5 台中 周五 1985
6 台中 吳六 1986
7 台中 鄭七 1987
8 高雄 馮九 1989
9 高雄 陳十 1990
以上,在處理(分隔橫排)前的直欄,即姓名、年次二者,皆為文字型別,故處理(分隔橫排)後的直欄,亦為文字型別。如要在分隔橫排處理時併同將指定直欄依其適用型別進行轉換,則要加convert = TRUE
為引數。下例經處理的年次直欄之型別改為整數。
> separate_rows(sr, 姓名, 年次, convert = TRUE)
Source: local data frame [9 x 3]
區域 姓名 年次
(chr) (chr) (int)
1 台北 趙一 1981
2 台北 錢二 1982
3 台北 孫三 1983
4 台北 李四 1984
5 台中 周五 1985
6 台中 吳六 1986
7 台中 鄭七 1987
8 高雄 馮九 1989
9 高雄 陳十 1990
unite()(聯合)
函式unite()
恰好與separate()
互補,其功能係聯合多個直欄為單一直欄,以下就此前'separate()'演示例之結果,將其年、月、日三直欄,以斜線為分隔符號,聯合為日期直欄,演示如下:
> unite(data = df_2, 日期, 年:日, sep = "/")
日期 台北 台中 高雄
1 2016/01/31 116 83 96
2 2016/02/29 121 84 87
3 2016/03/31 115 83 107
4 2016/04/30 130 78 100
5 2016/05/31 122 89 100
6 2016/06/30 115 82 106
nest()(套疊)
此一函式可將重覆值給套疊為串列變項(list-variable),即以串列作為數據框中的直欄,該作為直欄的串列其元素個數與所在數據框的橫排數目相同。以下先調入有名的 iris 數據,數據框有 5 個變項,其中 4 個為數值型別、1 個為因子型別,因子型別變項有 3 個層次,每個層次各有 50 個觀測例。
> data("iris")
> str(iris) # 檢視該數據框結構,共有 150 個觀測例(橫排)、5 個變項(直欄)。
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> summary(iris) # 總結該數據框,其中 Species 直欄有 3 個值(層次),每個層次有 50 個觀測例。
Sepal.Length Sepal.Width Petal.Length
Min. :4.300 Min. :2.000 Min. :1.000
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600
Median :5.800 Median :3.000 Median :4.350
Mean :5.843 Mean :3.057 Mean :3.758
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100
Max. :7.900 Max. :4.400 Max. :6.900
Petal.Width Species
Min. :0.100 setosa :50
1st Qu.:0.300 versicolor:50
Median :1.300 virginica :50
Mean :1.199
3rd Qu.:1.800
Max. :2.500
接下來我們將 iris 數據框中具有相同的 Species 值的其它各直攔數據,套疊為一數據框,而由於 Species 有 3 個值,故會產生 3 個數據框,其各數據框為套疊(串列)直欄的一個元素。待套疊直欄在函式中的引數得以x:z
代表自x
起至z
止的各直欄,或以-y
代表排除y
的其他全部直欄,而參數.key
則可以指定該函式所回傳數據框中套疊直欄的名稱。
> nest(iris, Sepal.Length:Petal.Width)
Source: local data frame [3 x 2]
Species data
(fctr) (chr)
1 setosa <tbl_df [50,4]>
2 versicolor <tbl_df [50,4]>
3 virginica <tbl_df [50,4]>
>
> iris_nested <- nest(iris, -Species, .key = 套疊數據)
> iris_nested
Source: local data frame [3 x 2]
Species 套疊數據
(fctr) (chr)
1 setosa <tbl_df [50,4]>
2 versicolor <tbl_df [50,4]>
3 virginica <tbl_df [50,4]>
經套疊的直欄,為一串列,串列中的各元素為一數據框。以下檢視該套疊串列內容。
> iris_nested$套疊數據
[[1]]
Source: local data frame [50 x 4]
Sepal.Length Sepal.Width Petal.Length Petal.Width
(dbl) (dbl) (dbl) (dbl)
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
7 4.6 3.4 1.4 0.3
8 5.0 3.4 1.5 0.2
9 4.4 2.9 1.4 0.2
10 4.9 3.1 1.5 0.1
.. ... ... ... ...
[[2]]
Source: local data frame [50 x 4]
Sepal.Length Sepal.Width Petal.Length Petal.Width
(dbl) (dbl) (dbl) (dbl)
1 7.0 3.2 4.7 1.4
2 6.4 3.2 4.5 1.5
3 6.9 3.1 4.9 1.5
4 5.5 2.3 4.0 1.3
5 6.5 2.8 4.6 1.5
6 5.7 2.8 4.5 1.3
7 6.3 3.3 4.7 1.6
8 4.9 2.4 3.3 1.0
9 6.6 2.9 4.6 1.3
10 5.2 2.7 3.9 1.4
.. ... ... ... ...
[[3]]
Source: local data frame [50 x 4]
Sepal.Length Sepal.Width Petal.Length Petal.Width
(dbl) (dbl) (dbl) (dbl)
1 6.3 3.3 6.0 2.5
2 5.8 2.7 5.1 1.9
3 7.1 3.0 5.9 2.1
4 6.3 2.9 5.6 1.8
5 6.5 3.0 5.8 2.2
6 7.6 3.0 6.6 2.1
7 4.9 2.5 4.5 1.7
8 7.3 2.9 6.3 1.8
9 6.7 2.5 5.8 1.8
10 7.2 3.6 6.1 2.5
.. ... ... ... ...
下面再以另一有名的 gapminder 數據來演示,先安裝套組包,再調入該套組,概覽其結構,檢視其總結。
> install.packages("gapminder")
> library(gapminder)
> str(gapminder)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1704 obs. of 6 variables:
$ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
$ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
$ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
$ lifeExp : num 28.8 30.3 32 34 36.1 ...
$ pop : int 8425333 9240934 10267083 11537966 13079460 14880372 12881816 ...
$ gdpPercap: num 779 821 853 836 740 ...
> summary(gapminder)
country continent year
Afghanistan: 12 Africa :624 Min. :1952
Albania : 12 Americas:300 1st Qu.:1966
Algeria : 12 Asia :396 Median :1980
Angola : 12 Europe :360 Mean :1980
Argentina : 12 Oceania : 24 3rd Qu.:1993
Australia : 12 Max. :2007
(Other) :1632
lifeExp pop gdpPercap
Min. :23.60 Min. :6.001e+04 Min. : 241.2
1st Qu.:48.20 1st Qu.:2.794e+06 1st Qu.: 1202.1
Median :60.71 Median :7.024e+06 Median : 3531.8
Mean :59.47 Mean :2.960e+07 Mean : 7215.3
3rd Qu.:70.85 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
Max. :82.60 Max. :1.319e+09 Max. :113523.1
先演示以不同洲別來套疊洲別以外的直欄數據,由於原數據內容在洲別直欄上有 5 個層次,故處理後得到 5 個橫排的數據框,接著檢視套疊直欄的第一個元素內容(數據框)。
> gapminder_continent_nested <- nest(gapminder, -continent)
> gapminder_continent_nested
Source: local data frame [5 x 2]
continent data
(fctr) (chr)
1 Asia <tbl_df [396,5]>
2 Europe <tbl_df [360,5]>
3 Africa <tbl_df [624,5]>
4 Americas <tbl_df [300,5]>
5 Oceania <tbl_df [24,5]>
>
> gapminder_continent_nested$data[[1]] # 檢視套疊直欄的第一個元素內容。
Source: local data frame [396 x 5]
country year lifeExp pop gdpPercap
(fctr) (int) (dbl) (int) (dbl)
1 Afghanistan 1952 28.801 8425333 779.4453
2 Afghanistan 1957 30.332 9240934 820.8530
3 Afghanistan 1962 31.997 10267083 853.1007
4 Afghanistan 1967 34.020 11537966 836.1971
5 Afghanistan 1972 36.088 13079460 739.9811
6 Afghanistan 1977 38.438 14880372 786.1134
7 Afghanistan 1982 39.854 12881816 978.0114
8 Afghanistan 1987 40.822 13867957 852.3959
9 Afghanistan 1992 41.674 16317921 649.3414
10 Afghanistan 1997 41.763 22227415 635.3414
.. ... ... ... ... ...
再演示以不同國家來套疊國家以外的直欄數據,由於原數據內容在洲別直欄上有 142 個層次,故處理後得到 142 個橫排的數據框,接著檢視套疊直欄的第一個元素內容(數據框)。
> gapminder_country_nested <- nest(gapminder, -country)
> gapminder_country_nested
Source: local data frame [142 x 2]
country data
(fctr) (chr)
1 Afghanistan <tbl_df [12,5]>
2 Albania <tbl_df [12,5]>
3 Algeria <tbl_df [12,5]>
4 Angola <tbl_df [12,5]>
5 Argentina <tbl_df [12,5]>
6 Australia <tbl_df [12,5]>
7 Austria <tbl_df [12,5]>
8 Bahrain <tbl_df [12,5]>
9 Bangladesh <tbl_df [12,5]>
10 Belgium <tbl_df [12,5]>
.. ... ...
> gapminder_country_nested$data[[1]] # 檢視套疊直欄的第一個元素內容。
Source: local data frame [12 x 5]
continent year lifeExp pop gdpPercap
(fctr) (int) (dbl) (int) (dbl)
1 Asia 1952 28.801 8425333 779.4453
2 Asia 1957 30.332 9240934 820.8530
3 Asia 1962 31.997 10267083 853.1007
4 Asia 1967 34.020 11537966 836.1971
5 Asia 1972 36.088 13079460 739.9811
6 Asia 1977 38.438 14880372 786.1134
7 Asia 1982 39.854 12881816 978.0114
8 Asia 1987 40.822 13867957 852.3959
9 Asia 1992 41.674 16317921 649.3414
10 Asia 1997 41.763 22227415 635.3414
11 Asia 2002 42.129 25268405 726.7341
12 Asia 2007 43.828 31889923 974.5803
unnest()(解套疊)
函式unnest()
恰好與nest()
相反,其功能係將套疊串列直欄給解開,回傳值為非套疊的數據框。以下將前述演示例 iris_nested、gapminder_continent_nested、gapminder_country_nested 的套疊結構給解除掉,原來套疊時作為區分橫排的因子直欄,成為回傳數據框的第一直欄,故後二者(gapminder_continent_nested、gapminder_country_nested)解套疊後的直欄排列順序不同。
> unnest(iris_nested)
Source: local data frame [150 x 5]
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
(fctr) (dbl) (dbl) (dbl) (dbl)
1 setosa 5.1 3.5 1.4 0.2
2 setosa 4.9 3.0 1.4 0.2
3 setosa 4.7 3.2 1.3 0.2
4 setosa 4.6 3.1 1.5 0.2
5 setosa 5.0 3.6 1.4 0.2
6 setosa 5.4 3.9 1.7 0.4
7 setosa 4.6 3.4 1.4 0.3
8 setosa 5.0 3.4 1.5 0.2
9 setosa 4.4 2.9 1.4 0.2
10 setosa 4.9 3.1 1.5 0.1
.. ... ... ... ... ...
>
> unnest(gapminder_continent_nested)
Source: local data frame [1,704 x 6]
continent country year lifeExp pop gdpPercap
(fctr) (fctr) (int) (dbl) (int) (dbl)
1 Asia Afghanistan 1952 28.801 8425333 779.4453
2 Asia Afghanistan 1957 30.332 9240934 820.8530
3 Asia Afghanistan 1962 31.997 10267083 853.1007
4 Asia Afghanistan 1967 34.020 11537966 836.1971
5 Asia Afghanistan 1972 36.088 13079460 739.9811
6 Asia Afghanistan 1977 38.438 14880372 786.1134
7 Asia Afghanistan 1982 39.854 12881816 978.0114
8 Asia Afghanistan 1987 40.822 13867957 852.3959
9 Asia Afghanistan 1992 41.674 16317921 649.3414
10 Asia Afghanistan 1997 41.763 22227415 635.3414
.. ... ... ... ... ... ...
> unnest(gapminder_country_nested)
Source: local data frame [1,704 x 6]
country continent year lifeExp pop gdpPercap
(fctr) (fctr) (int) (dbl) (int) (dbl)
1 Afghanistan Asia 1952 28.801 8425333 779.4453
2 Afghanistan Asia 1957 30.332 9240934 820.8530
3 Afghanistan Asia 1962 31.997 10267083 853.1007
4 Afghanistan Asia 1967 34.020 11537966 836.1971
5 Afghanistan Asia 1972 36.088 13079460 739.9811
6 Afghanistan Asia 1977 38.438 14880372 786.1134
7 Afghanistan Asia 1982 39.854 12881816 978.0114
8 Afghanistan Asia 1987 40.822 13867957 852.3959
9 Afghanistan Asia 1992 41.674 16317921 649.3414
10 Afghanistan Asia 1997 41.763 22227415 635.3414
.. ... ... ... ... ... ...
>