【发布时间】:2020-03-10 21:14:11
【问题描述】:
这必须与此处描述的相似
https://github.com/tidyverse/purrr/issues/179
但我的头撞墙了。
考虑sn-p
library(tidyverse)
library(stringr)
library(stringi)
remove_short_words <- function(x, n){
mypattern <- paste("\\w{", n, ",}", sep="")
x2 <- paste(str_extract_all(x, mypattern)[[1]], collapse=' ')
}
shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
dd<-map2(shopping_list, 4, function(x,y) remove_short_words(x,y))
ff<-map2_df(shopping_list, 4, function(x,y) remove_short_words(x,y))
map2 像一个魅力,但 map2_df 抛出一个错误
Error: Argument 1 must have names
知道如何解决这个问题吗? 谢谢!
#我添加对我有用的东西。我使用插入地图的辅助功能
remove_short_words_aux <- function(x, n){
mypattern <- paste("\\w{", n, ",}", sep="")
x2 <- paste(str_extract_all(x, mypattern)[[1]], collapse=' ')
}
remove_short_words <- function(x,n){
res<-map(x, function(x) remove_short_words_aux(x,n)) %>%
unlist %>%
tibble::enframe(name = NULL)
}
> nn<-remove_short_words(shopping_list,5)
> nn
# A tibble: 4 x 1
value
<chr>
1 "apples"
2 "flour"
3 "sugar"
4 ""
【问题讨论】: