開始使用免費開始

NHANES 資料集建置

NHANES 網站 下載時,NHANES 資料集僅提供為個別的 .XPT 檔案,這是 SAS 的原生格式。所幸我們可以使用 haven 套件。

讓我們把 NHANES 的人口統計(Demographics)、病史(Medical Conditions)以及身體測量(Body Measures)這 3 個資料集結合起來。這些資料以原始 .XPT 格式提供,並可透過 DEMO_fileMCQ_fileBMX_file 這三個變數存取。請用變數 SEQN 將 3 個資料集合併。使用 Reduce() 是個不錯的方法,它能以便利的方式把元素合併。

提供給你的合併程式碼會執行以下動作:

  • 建立由 3 個資料集(nhanes_demonhanes_medicalnhanes_bodymeasures)組成的清單。
  • Reduce() 中使用自訂函式,利用 "SEQN" 變數對 3 個資料集做 inner join。
  • 把結果儲存為 nhanes_combined 資料集。

本練習屬於課程

R 的實驗設計

檢視課程

練習說明

  • 載入 haven 套件。
  • 以 3 次分別呼叫 read_xpt() 匯入三個資料檔,這三次呼叫的輸入分別為 DEMO_fileMCQ_fileBMX_file,並各自儲存為 nhanes_demonhanes_medicalnhanes_bodymeasures 資料集。
  • 使用提供的程式碼,將你剛匯入的 3 個資料集合併為 nhanes_combined

動手互動練習

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

# Load haven
___

# Import the three datasets using read_xpt()
nhanes_demo <- read_xpt(DEMO_file)
___
___

# Merge the 3 datasets you just created to create nhanes_combined
nhanes_combined <- list(nhanes_demo, nhanes_medical, nhanes_bodymeasures) %>%
  Reduce(function(df1, df2) inner_join(df1, df2, by = "SEQN"), .)
編輯並執行程式碼