我有什么:
DF1
<表类>
应用
Proc
Rej
老
tbody><<tr>2021-05-11 2021-05-11 NA NA 2021-05-11 2021-05-11 NA NA 2021-05-11 NA NA NA 2021-05-11 2021-05-11 NA NA 2021-05-12 2021-05-12 NA 2021-09-12 2021-05-12 2021-05-12 2021-10-01 NA 2021-05-13 2021-05-13 NA 2021-09-12 2021-05-13 2021-05-13 2021-10-01 NA 2021-05-13 2021-05-13 2021-10-01 NA 2021-05-13 NA NA NA 2021-05-13 2021-05-13 NA 2021-09-12 2021-05-13 NA NA NA 2021-05-13 2021-05-13 NA NA 表类>
试试这个
library(dplyr , warn.conflicts = F)
DF1 |> group_by(App) |> summarise(app = n()) -> df2
DF1 |> group_by(Proc) |> summarise(proc =n()) -> df3
df2 |> left_join(df3 , by = c("App" = "Proc")) -> DFdesired
DFdesired
# A tibble: 3 × 3
App app proc
<chr> <int> <int>
1 2021-05-11 4 3
2 2021-05-12 2 2
3 2021-05-13 7 5
这里是使用tidyr
中的pivot_longer
和pivot_wider
的另一种方法。您可以选择App
和Proc
列进行透视,并删除NA
为值的行。
library(tidyverse)
df1 %>%
pivot_longer(cols = c(App, Proc), values_drop_na = T) %>%
count(value, name) %>%
pivot_wider(id_cols = value, names_from = name, values_from = n)
value App Proc
<chr> <int> <int>
1 2021-05-11 4 3
2 2021-05-12 2 2
3 2021-05-13 7 5