是否有可以计算非数字列出现次数的 R 函数?
Is there an R function that can count the occurrences of a non-numeric column?
我在计算分类列时遇到问题。提前致谢!
所以我有这个数据框:
Employee ID Station
1001 Produce
1002 Pharmacy
1001 Frozen
1003
我想在数据框中添加一列来计算每个员工拥有多少个站点。
输出示例:
Employee ID Station Count
1001 Produce 2
1002 Pharmacy 1
1001 Frozen 2
1003 0
包 dplyr
中的函数 count
就是为了这个:
library(tidyverse)
data <- tribble(
~Employee.ID, ~Station,
1001L, "Produce",
1002L, "Pharmacy",
1001L, "Frozen",
1003L, NA
)
counts <-
data %>%
filter(!is.na(Station)) %>%
count(Employee.ID)
counts
#> # A tibble: 2 x 2
#> Employee.ID n
#> <int> <int>
#> 1 1001 2
#> 2 1002 1
data %>%
left_join(counts) %>%
mutate(n = n %>% replace_na(0))
#> Joining, by = "Employee.ID"
#> # A tibble: 4 x 3
#> Employee.ID Station n
#> <int> <chr> <dbl>
#> 1 1001 Produce 2
#> 2 1002 Pharmacy 1
#> 3 1001 Frozen 2
#> 4 1003 <NA> 0
由 reprex package (v2.0.1)
于 2021-12-08 创建
我在计算分类列时遇到问题。提前致谢!
所以我有这个数据框:
Employee ID Station
1001 Produce
1002 Pharmacy
1001 Frozen
1003
我想在数据框中添加一列来计算每个员工拥有多少个站点。
输出示例:
Employee ID Station Count
1001 Produce 2
1002 Pharmacy 1
1001 Frozen 2
1003 0
包 dplyr
中的函数 count
就是为了这个:
library(tidyverse)
data <- tribble(
~Employee.ID, ~Station,
1001L, "Produce",
1002L, "Pharmacy",
1001L, "Frozen",
1003L, NA
)
counts <-
data %>%
filter(!is.na(Station)) %>%
count(Employee.ID)
counts
#> # A tibble: 2 x 2
#> Employee.ID n
#> <int> <int>
#> 1 1001 2
#> 2 1002 1
data %>%
left_join(counts) %>%
mutate(n = n %>% replace_na(0))
#> Joining, by = "Employee.ID"
#> # A tibble: 4 x 3
#> Employee.ID Station n
#> <int> <chr> <dbl>
#> 1 1001 Produce 2
#> 2 1002 Pharmacy 1
#> 3 1001 Frozen 2
#> 4 1003 <NA> 0
由 reprex package (v2.0.1)
于 2021-12-08 创建