Reputation: 18830
Working with a data set that has user_ids
and number_of_visits
they have visited a specific location (geo_location). I am trying to get a bar plot where y
axis reflecting total_number_of_users
and x
reflecting condition such as:
10 or more visits
user_id, number_of_visits
0001, 12
0002, 3
0003, 1
0004, 34
0005, 11
0006, 8
using count seems like an overkill since it creates logical table. Since no grouping involve use of dplyr seems like not an option.
count(df, number_of_visits >= 3, number_of_visits >=5)
df %>% number_of_visits(count3 = number_of_visits >= 3)
Upvotes: 0
Views: 311
Reputation: 2403
library(tidyverse)
df = data.frame(user_id = c("0001", "0002", "0003", "0004", "0005", "0006"),
number_of_visits = c(12,3,1,34,11,8))
df = df %>%
mutate(threeOrMore = ifelse(number_of_visits >= 3, ">=3", "<3")) %>%
mutate(fiveOrMore = ifelse(number_of_visits >= 5, ">=5", "<5")) %>%
mutate(tenOrMore = ifelse(number_of_visits >= 10, ">=10", "<10"))
plot = ggplot(df) +
geom_bar(aes(threeOrMore))
print(plot)
Upvotes: 2