r中某变量的日期计数

  • 本文关键字:日期 变量 r date count
  • 更新时间 :
  • 英文 :


我有什么:

DF1

<表类> 应用 Proc Rej 老 tbody><<tr>2021-05-112021-05-11NANA2021-05-112021-05-11NANA2021-05-11NANANA2021-05-112021-05-11NANA2021-05-122021-05-12NA2021-09-122021-05-122021-05-122021-10-01NA2021-05-132021-05-13NA2021-09-122021-05-132021-05-132021-10-01NA2021-05-132021-05-132021-10-01NA2021-05-13NANANA2021-05-132021-05-13NA2021-09-122021-05-13NANANA2021-05-132021-05-13NANA

试试这个

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_longerpivot_wider的另一种方法。您可以选择AppProc列进行透视,并删除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

相关内容

  • 没有找到相关文章

最新更新