我有一個包含每日資料的龐大面板資料集。我想洗掉“大小”列中缺失超過 25% 觀察資料的所有日期。
我創建了以下資料來顯示我的真實資料的樣子:
structure(list(Product = c("A", "A", "A", "A", "A", "A", "A",
"A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C",
"C", "C", "C", "C", "C", "C", "C"), Date = c("01.09.2018", "02.09.2018",
"03.09.2018", "04.09.2018", "05.09.2018", "11.11.2020", "12.11.2020",
"13.11.2020", "14.11.2020", "01.09.2018", "02.09.2018", "03.09.2018",
"04.09.2018", "05.09.2018", "11.11.2020", "12.11.2020", "13.11.2020",
"14.11.2020", "01.09.2018", "02.09.2018", "03.09.2018", "04.09.2018",
"05.09.2018", "11.11.2020", "12.11.2020", "13.11.2020", "14.11.2020"
), Size = c(10L, 9L, NA, 3L, 4L, 5L, 3L, NA, 6L, 7L, 4L, NA,
4L, 6L, 6L, 4L, 6L, 7L, 3L, 4L, NA, 2L, 4L, NA, 7L, 7L, 5L)), class = "data.frame", row.names = c(NA,
-27L))
我已經嘗試了以下方法,但我對如何繼續使用代碼有所了解:
Data %>% summarize(group_by(Date), NoData=(is.na(Size))
然后我得到一個錯誤,我不能將 group_by 用于“日期”類的物件。此外,我不知道如何自動洗掉“大小”列中缺失值超過 25% 的天數。
任何人都可以在這里幫助我解決適用于我的問題的代碼嗎?
我感謝您的幫助。
uj5u.com熱心網友回復:
如果您summarize(),您會在個別日子丟失大量資訊。此外,group_by()在進一步的 dplyr 動詞之前使用。您可以通過將 NA 的總和除以天數的總和來計算 NA 的百分比。as_tibble()僅用于更好地顯示行數,沒有它也可以。我添加了一列“CountDate”,以便您知道同一天在您的資料框中出現了多少次。
Data %>% as_tibble() %>%
group_by(Date) %>%
mutate(CountDate = n(), PercNA = sum(is.na(Size))/n()*100)
# A tibble: 27 x 5
# Groups: Date [9]
Product Date Size CountDate PercNA
<chr> <chr> <int> <int> <dbl>
1 A 01.09.2018 10 3 0
2 A 02.09.2018 9 3 0
3 A 03.09.2018 NA 3 100
4 A 04.09.2018 3 3 0
5 A 05.09.2018 4 3 0
6 A 11.11.2020 5 3 33.3
7 A 12.11.2020 3 3 0
8 A 13.11.2020 NA 3 33.3
9 A 14.11.2020 6 3 0
10 B 01.09.2018 7 3 0
# ... with 17 more rows
要洗掉具有 >25% NA 的日期,只需filter():
Data %>% as_tibble() %>%
group_by(Date) %>%
mutate(CountDate = n(), PercNA = sum(is.na(Size))/n()*100) %>%
filter(PercNA <25) %>%
ungroup()
# A tibble: 18 x 5
Product Date Size CountDate PercNA
<chr> <chr> <int> <int> <dbl>
1 A 01.09.2018 10 3 0
2 A 02.09.2018 9 3 0
3 A 04.09.2018 3 3 0
4 A 05.09.2018 4 3 0
5 A 12.11.2020 3 3 0
6 A 14.11.2020 6 3 0
7 B 01.09.2018 7 3 0
8 B 02.09.2018 4 3 0
9 B 04.09.2018 4 3 0
10 B 05.09.2018 6 3 0
11 B 12.11.2020 4 3 0
12 B 14.11.2020 7 3 0
13 C 01.09.2018 3 3 0
14 C 02.09.2018 4 3 0
15 C 04.09.2018 2 3 0
16 C 05.09.2018 4 3 0
17 C 12.11.2020 7 3 0
18 C 14.11.2020 5 3 0
uj5u.com熱心網友回復:
@Gnueghoidune 的回答非常好。我只想補充一點,可以直接在里面寫filter(),從而避免使用summarize()and mutate()。
下面是一個例子(我用的是mean(is.na(x))代替sum(is.na(x))/n()):
library(dplyr)
Data %>%
group_by(Date) %>%
filter(mean(is.na(Size)) <= 0.25)
#> # A tibble: 18 × 3
#> # Groups: Date [6]
#> Product Date Size
#> <chr> <chr> <int>
#> 1 A 01.09.2018 10
#> 2 A 02.09.2018 9
#> 3 A 04.09.2018 3
#> 4 A 05.09.2018 4
#> 5 A 12.11.2020 3
#> 6 A 14.11.2020 6
#> 7 B 01.09.2018 7
#> 8 B 02.09.2018 4
#> 9 B 04.09.2018 4
#> 10 B 05.09.2018 6
#> 11 B 12.11.2020 4
#> 12 B 14.11.2020 7
#> 13 C 01.09.2018 3
#> 14 C 02.09.2018 4
#> 15 C 04.09.2018 2
#> 16 C 05.09.2018 4
#> 17 C 12.11.2020 7
#> 18 C 14.11.2020 5
由reprex 包于 2022-04-15 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/461650.html
上一篇:在R中迭代時,日期被轉換為數字
下一篇:從另一個函式功能性地創建陣列
