之前今天我在這里發布了這個問題,但我還有一個問題已經提出。
如果我必須在原始資料框中添加更多變數(loc,height):
var = c(rep("A",3),rep("B",2),rep("C",5));var
date = c(as.Date("2022/01/01"),as.Date("2022/02/01"),as.Date("2022/03/01"),
as.Date("2022/01/01"),as.Date("2022/03/01"),
as.Date("2022/01/01"),as.Date("2022/01/01"),as.Date("2022/02/01"),as.Date("2022/02/01"),as.Date("2022/03/01"))
loc = c(rep("london",3),rep("berlin",2),rep("cairo",5))
height =c(13,14,15,13,15,16,12,14,13,15)
data = tibble(var,date,loc,height);data
我怎樣才能保持 loc 變數的相應值和每個月的總高度(總和)(除了前面回答的問題)?
理想情況下,它現在必須看起來像這樣:
| 變數 | 四分之一 | 月 | 健康)狀況 | 位置 | 高度 |
|---|---|---|---|---|---|
| 一個 | 1 | 1 | 真的 | 倫敦 | 13 |
| 一個 | 1 | 2 | 真的 | 倫敦 | 14 |
| 一個 | 1 | 3 | 真的 | 倫敦 | 15 |
| 乙 | 1 | 1 | 真的 | 柏林 | 13 |
| 乙 | 1 | 2 | 錯誤的 | 柏林 | 0 |
| 乙 | 1 | 3 | 真的 | 柏林 | 15 |
| C | 1 | 1 | 真的 | 開羅 | 28 |
| C | 1 | 2 | 真的 | 開羅 | 27 |
| C | 1 | 3 | 真的 | 開羅 | 15 |
有什么幫助嗎?我如何使用 dplyr 在 R 中做到這一點?
uj5u.com熱心網友回復:
從先前的解決方案中,添加.keep_all = TRUE,distinct然后添加具有先前非 NA 值fill的列loc
library(dplyr)
library(tidyr)
library(lubridate)
data %>%
mutate(month = lubridate::month(date)) %>%
group_by(var, month) %>%
mutate(height = sum(height)) %>%
ungroup %>%
complete(var, month, fill = list(height = 0)) %>%
mutate(Quarter = quarter, Condition = !is.na(date)) %>%
distinct(var, month, Quarter, Condition, .keep_all = TRUE) %>%
fill(loc) %>%
select(-date)
-輸出
# A tibble: 9 × 6
var month loc height Quarter Condition
<chr> <dbl> <chr> <dbl> <dbl> <lgl>
1 A 1 london 13 1 TRUE
2 A 2 london 14 1 TRUE
3 A 3 london 15 1 TRUE
4 B 1 berlin 13 1 TRUE
5 B 2 berlin 0 1 FALSE
6 B 3 berlin 15 1 TRUE
7 C 1 cairo 28 1 TRUE
8 C 2 cairo 27 1 TRUE
9 C 3 cairo 15 1 TRUE
uj5u.com熱心網友回復:
基于先前的答案:
data <- data %>% mutate(month=month(date),quarter=quarter(month))
left_join(
expand(data, var,month,quarter),
data %>% group_by(var,month, date,loc) %>%
summarize(height=sum(height), .groups="drop") %>%
select(-date) %>%
mutate(condition=TRUE)
) %>%
mutate(condition=!is.na(condition),height=if_else(is.na(height),0,height)) %>%
group_by(var) %>% fill(loc)
輸出:
var month quarter loc height condition
<chr> <dbl> <int> <chr> <dbl> <lgl>
1 A 1 1 london 13 TRUE
2 A 2 1 london 14 TRUE
3 A 3 1 london 15 TRUE
4 B 1 1 berlin 13 TRUE
5 B 2 1 berlin 0 FALSE
6 B 3 1 berlin 15 TRUE
7 C 1 1 cairo 28 TRUE
8 C 2 1 cairo 27 TRUE
9 C 3 1 cairo 15 TRUE
uj5u.com熱心網友回復:
這是dplyr解決方案:這部分complete(var,Month, fill = list(height = 0))來自@akrun:
library(dplyr)
data %>%
group_by(var, Quarter = quarter(date), Month = month(date), loc) %>%
summarise(height = sum(height)) %>%
ungroup() %>%
complete(var,Month, fill = list(height = 0)) %>%
fill(c(Quarter, loc), .direction = "down") %>%
mutate(Condition = ifelse(height == 0 , FALSE, TRUE))
var Month Quarter loc height Condition
<chr> <dbl> <int> <chr> <dbl> <lgl>
1 A 1 1 london 13 TRUE
2 A 2 1 london 14 TRUE
3 A 3 1 london 15 TRUE
4 B 1 1 berlin 13 TRUE
5 B 2 1 berlin 0 FALSE
6 B 3 1 berlin 15 TRUE
7 C 1 1 cairo 28 TRUE
8 C 2 1 cairo 27 TRUE
9 C 3 1 cairo 15 TRUE
uj5u.com熱心網友回復:
您可以nesting()在complete()步驟中使用僅獲取您想要的組合,然后在對高度求和之前按您想要保持不同的所有內容進行分組:
library(dplyr)
library(tidyr)
var = c(rep("A",3),rep("B",2),rep("C",5));var
date = c(as.Date("2022/01/01"),as.Date("2022/02/01"),as.Date("2022/03/01"),
as.Date("2022/01/01"),as.Date("2022/03/01"),
as.Date("2022/01/01"),as.Date("2022/01/01"),as.Date("2022/02/01"),as.Date("2022/02/01"),as.Date("2022/03/01"))
loc = c(rep("london",3),rep("berlin",2),rep("cairo",5))
height =c(13,14,15,13,15,16,12,14,13,15)
data = tibble(var,date,loc,height)
data %>%
mutate(month = lubridate::month(date)) %>%
complete(month, nesting(var, loc)) %>%
mutate(Quarter = lubridate::quarter(month),
Condition = !is.na(date)) %>%
group_by(across(-c(height, date))) %>%
summarise(height = sum(height), .groups = "drop") %>%
arrange(var)
#> # A tibble: 9 × 6
#> month var loc Quarter Condition height
#> <dbl> <chr> <chr> <int> <lgl> <dbl>
#> 1 1 A london 1 TRUE 13
#> 2 2 A london 1 TRUE 14
#> 3 3 A london 1 TRUE 15
#> 4 1 B berlin 1 TRUE 13
#> 5 2 B berlin 1 FALSE NA
#> 6 3 B berlin 1 TRUE 15
#> 7 1 C cairo 1 TRUE 28
#> 8 2 C cairo 1 TRUE 27
#> 9 3 C cairo 1 TRUE 15
由reprex 包于 2022-06-01 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/484926.html
上一篇:如果向量的值對應(存在)到資料框的列,我如何使用dplyr檢查R?
下一篇:從資料框中創建命名元組串列
