開始使用免費開始

族群趨勢的相對變化

在上一個練習中,你比較了不同族群的借貸隨時間的變化。這個練習要觀察族群趨勢的相對變化。做法是把每個族群的趨勢,以第一年(2008 年)的借貸做標準化。

本練習屬於課程

R 的可擴展資料處理

檢視課程

練習說明

  • 取得 rydf 的第一欄並命名為 column1
  • 1:8 欄進行迴圈,將每一欄除以第一欄來做標準化。
  • 繪圖時,將 Year 放在 x 軸、Proportion 放在 y 軸,並依 Race 變數設定顏色與群組。

動手互動練習

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

# View rydf
rydf

# Get the first column of rydf
column1 <- rydf[, 1]

# Normalize the first 8 columns
for(this_column in 1:8) {
  rydf[, this_column] <- rydf[, ___] / ___
}

# Convert the data to long format
rydf_long <- pivot_longer(rydf, -Race, names_to = "Year", values_to = "Proportion")

# Plot
ggplot(rydf_long, aes(x = ___, y = ___, group = Race, color = Race)) + 
    geom_line()
編輯並執行程式碼