使用具有user_ids
和number_of_visits
的数据集,他们访问了特定位置(geo_location)。我正在尝试获得一个条形图,其中y
轴反映total_number_of_users
和x
反映条件,例如:
10次或更多次访问
user_id, number_of_visits
0001, 12
0002, 3
0003, 1
0004, 34
0005, 11
0006, 8
使用count似乎是一种过度杀伤,因为它创建了逻辑表。由于没有涉及使用dplyr的分组似乎不是一种选择。
count(df, number_of_visits >= 3, number_of_visits >=5)
df %>% number_of_visits(count3 = number_of_visits >= 3)
答案 0 :(得分:2)
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)