For each year, I would like to create two new columns temp_count
and rh_count
counting the number of occurrences in each temp_catog
and humidity_catog
columns, respectively. This How to count how many values per level in a given factor? answers if you group by one variable, but I would like to use group_by(year, humidity_catog, temp_catog)
. Here is a screenshot of my data
I can a create a single column humidity_count
to count number of occurrences in each category humidity_catog
column using the following code.
df <- group_by(year, humidity_catog) %>%
summarize(humidity_count = n())
Here is the output
But I would like to create another column temp_count
in the same data frame to count the number of each category temp_count
column. How can I achieve this? Here is the reproducible example of my data created via dput function.
df <- structure(
list(
year = structure(
c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L),
.Label = c(
"2006",
"2007",
"2012",
"2013",
"2014",
"2014_c",
"2015_a",
"2015_b",
"2016",
"2017",
"2020"
),
class = "factor"
),
min_rh = c(47.9, 49, 44.7, 40.2, 50, 52.3, 51.5, 82.8, 73.8,
47.1),
min_temp = c(12.4, 14.3, 15.1, 16.1, 12.7, 16.1, 14.4,
15.1, 11.8, 9.5),
temp_catog = structure(
c(2L, 2L, 3L, 3L,
2L, 3L, 2L, 3L, 2L, 2L),
.Label = c("T1(<=8)", "T2(>8, <=15)",
"T3(>15)"),
class = "factor"
),
humidity_catog = structure(
c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L),
.Label = c("RH1(<=65)",
"RH2(>65)"),
class = "factor"
)
),
class = c("grouped_df",
"tbl_df", "tbl", "data.frame"),
row.names = c(NA,-10L),
groups = structure(
list(
year = structure(
1L,
.Label = c(
"2006",
"2007",
"2012",
"2013",
"2014",
"2014_c",
"2015_a",
"2015_b",
"2016",
"2017",
"2020"
),
class = "factor"
),
.rows = structure(
list(1:10),
ptype = integer(0),
class = c("vctrs_list_of",
"vctrs_vctr", "list")
)
),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-1L),
.drop = TRUE
)
)
Note: I don't want unique occurrences. I just want to count how many times each category was recorded.