開始使用免費開始

清單欄(List columns)

這個「巢狀」資料有一個有趣的結構。第二欄 data 是一個「清單」(list),這是一種本課尚未介紹的 R 物件,能在每一列中儲存較複雜的物件。原因是 data 欄中的每個項目本身都是一個 data frame。

# A tibble: 200 × 2
                           country              data
                             <chr>            <list>
1                      Afghanistan <tibble [34 × 3]>
2                        Argentina <tibble [34 × 3]>
3                        Australia <tibble [34 × 3]>
4                          Belarus <tibble [34 × 3]>
5                          Belgium <tibble [34 × 3]>
6  Bolivia, Plurinational State of <tibble [34 × 3]>
7                           Brazil <tibble [34 × 3]>
8                           Canada <tibble [34 × 3]>
9                            Chile <tibble [34 × 3]>
10                        Colombia <tibble [34 × 3]>

你可以用 nested$data 存取這個清單欄,並用雙中括號來取得特定元素。例如,nested$data[[1]] 會給你 Afghanistan 的投票歷史資料(每年 percent_yes),因為 Afghanistan 是表格的第 1 列。

本練習屬於課程

案例研究:R 中的探索性資料分析

檢視課程

練習說明

印出 data 欄中包含 Brazil 資料的那個 data frame。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# All countries are nested besides country
nested <- by_year_country %>%
  nest(-country)

# Print the nested data for Brazil
編輯並執行程式碼