有多少遺漏值?
在開始查看新的資料集時,你首先會想確認是否存在遺漏值,以及其數量。
你可以用 are_na() 來計算遺漏值的數量,不過,計算遺漏值「最有效率的方法」是使用 n_miss() 函式。它會回傳資料中的「遺漏值總數」。
接著,你可以用 pct_miss 函式找出資料中的遺漏值比例。它會回傳資料中的「遺漏百分比」。
此外,你也可以用 n_complete 與 pct_complete 來取得相對的資訊——也就是「完整值」的數量與比例。
本練習屬於課程
在 R 中處理遺漏值
練習說明
使用身高與體重的範例資料框 dat_hw:
- 對資料框
dat_hw使用n_miss(),計算整個資料框中的遺漏值總數。 - 對變數
dat_hw$weight使用n_miss(),計算該變數中的遺漏值總數。 - 同樣地,使用
prop_miss()、n_complete()與prop_complete(),分別取得遺漏值的比例,以及資料框與各變數的完整值數量與比例。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Use n_miss() to count the total number of missing values in dat_hw
n_miss(___)
# Use n_miss() on dat_hw$weight to count the total number of missing values
n_miss(___$___)
# Use n_complete() on dat_hw to count the total number of complete values
n_complete(___)
# Use n_complete() on dat_hw$weight to count the total number of complete values
___(___$___)
# Use prop_miss() and prop_complete() on dat_hw to count the total number of missing values in each of the variables
prop_miss(____)
prop_complete(___)