r - 用户在 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
推荐阅读
- text - 在 Qlikview 中四舍五入文本
- docker - 为什么 docker 不在 0.0.0.0 上运行烧瓶应用程序?
- android - Facebook 和 Google 登录在 .aab 发布版本中不起作用。但是以 .apk 格式创建 realse 构建效果很好
- php - 如何将整数值转换为时间戳格式?
- javascript - 如何使用 antd 为反应应用程序创建多复选框
- amazon-web-services - 撤销所有用户的所有刷新令牌。认知
- python - 熊猫:如何重命名一列但不丢失熊猫以前的文本?
- java - 如何使用 DateTimeFormatter 转换字符串以获取 java.sql.Timestamp
- angular - 将特定领域的服务作为 Angular 通用服务的一部分使用的推荐方法是什么?
- ios - 这里适用于 iOS 的 SDK lite 包含不受支持的文件