'Group by and Count character values R
I need to count how many orders is per date and how many "yes" and "no" for each date. How can I from the data frame like this:
order <- c("order_1", "order_2","order_3","order_4","order_5")
check <- c("yes", "no","no","yes","yes")
df1 <- data.frame(order, date,check )
df1
order date check
1 order_1 2020-01-01 yes
2 order_2 2020-01-01 no
3 order_3 2020-01-01 no
4 order_4 2020-01-02 yes
5 order_5 2020-01-02 yes
Get table like this:
date number_orders yes no
1 2020-01-01 3 1 2
2 2020-01-02 2 0 0
´´´
Solution 1:[1]
Start by adding your yes and no columns to the dataframe. Let yes be true whenever check == 'yes' and false otherwise, and let no be true whenever check == 'no' and false otherwise.
df1 <- df1 %>% mutate(yes = (check == 'yes'), no = (check == 'no'))
Now group by date and use summarize() to count up the number of orders. You can take advantage of the fact that TRUE evaluates to 1 and FALSE evaluates to 0 to sum over the yes and no columns.
df1 <- df1 %>%
group_by(date) %>%
summarise(number_orders = n(),
yes = sum(yes),
no = sum(no))
The full code can be written as one line with dplyr's pipe operation.
df1 <- df1 %>% mutate(yes = (check == 'yes'), no = (check == 'no')) %>%
group_by(date) %>%
summarise(number_orders = n(),
yes = sum(yes),
no = sum(no))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hannah Verdonk |
