用户在 R 中撰写的平均帖子数

Average number of posts written by user in R

我有一个包含文本和 user_type 的数据框。我想获得 user_type.

发表的平均帖子数

数据:

df$post
[1] "hi my name is"
[2] "hey how are you"
[3] "whats up"

df$user_type

[1] big_user
[2] big_user
[3] small_user

使用dplyr:

# counting the posts per user
df %>%
  count(user_type)
#    user_type n
# 1   big_user 2
# 2 small_user 1

# calculating the average length of posts per user
df %>% 
  group_by(user_type) %>%
  summarise(average = sum(str_count(text))/n(), .groups="drop")
#   A tibble: 2 x 2
#   user_type  average
#   <chr>        <dbl>
# 1 big_user        14
# 2 small_user       8

使用base R:

table(df$user_type)
#   big_user small_user 
#          2          1