我想生成一个dplyr管道中字数统计频率的频率计数摘要.它必须在dplyr管道中,因为我实际上是在查询bigrquery并且它充当dplyr管道.
假设我有这样的数据:
tf1 <- tbl_df(data.frame(row= c(1:5), body=c("tt t ttt j ss oe", "kpw eero", "pow eir sap r", "s", "oe")))
Run Code Online (Sandbox Code Playgroud)
我想得到一个关于字数的总结(类似这样):
n_words freq
1 0 0
2 1 2
3 2 1
4 3 0
5 4 1
6 5 0
7 6 1
Run Code Online (Sandbox Code Playgroud)
但我需要在dplyr管道中执行此操作(类似于下面的操作不起作用)
###NOT WORK
tf1 %>%
wordcount(body,sep=" ", count.function=sum)
Run Code Online (Sandbox Code Playgroud)
这是另一个complete用于获取所有值的想法,
library(tidyverse)
tf1 %>%
mutate(n_words = stringr::str_count(body, ' ') + 1) %>%
count(n_words) %>%
complete(n_words = 0:max(n_words))
Run Code Online (Sandbox Code Playgroud)
这使,
Run Code Online (Sandbox Code Playgroud)# A tibble: 7 x 2 n_words n <dbl> <int> 1 0. NA 2 1. 2 3 2. 1 4 3. NA 5 4. 1 6 5. NA 7 6. 1
| 归档时间: |
|
| 查看次数: |
444 次 |
| 最近记录: |