使用更新的样本数据
vec <- c("hi, how are you doing?",
"what time is it?",
"the sky is blue",
"hi, how are you doing today? You seem tired.",
"walk the dog",
"the grass is green",
"the sky is blue during the day",
"12/7/2018",
"8/12/2018",
"9/9/2016")
代码
library( tidyverse )
df <- data.frame( text = vec, stringsAsFactors = FALSE )
#greate group_indices
df$group_id <- df %>% group_indices( stringr::word( text, start = 1, end = 4) )
df %>%
#create some helping variables
mutate( length = str_count( text, " ") + 1,
row_id = row_number() ) %>%
#now group on id
group_by( group_id ) %>%
#arrange by group on length (descending)
arrange( -length ) %>%
#keep only the first row (of every group ), also keep all strings shorter than 4 words
filter( (row_number() == 1L & length >= 4) | length < 4 ) %>%
ungroup() %>%
#set back to the original order
arrange( row_id ) %>%
select( text )
输出
# # A tibble: 8 x 1
# text
# <chr>
# 1 what time is it?
# 2 hi, how are you doing today? You seem tired.
# 3 walk the dog
# 4 the grass is green
# 5 the sky is blue during the day
# 6 12/7/2018
# 7 8/12/2018
# 8 9/9/2016