【问题标题】:Map2_df and named argumentsMap2_df 和命名参数
【发布时间】: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 ""     


【问题讨论】:

    标签: r dplyr purrr stringr


    【解决方案1】:

    首先,我认为您不应该在这里使用map2,因为n 是固定的并且不会更改,因此使用map 是合适的。

    library(tidyverse)
    
    remove_short_words <- function(x, n){
      mypattern <- paste("\\w{", n, ",}", sep="")
      paste(str_extract_all(x, mypattern)[[1]], collapse=' ')
    }
    
    shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
    map(shopping_list, remove_short_words, 4)
    
    #[[1]]
    #[1] "apples"
    
    #[[2]]
    #[1] "flour"
    
    #[[3]]
    #[1] "sugar"
    
    #[[4]]
    #[1] "milk"
    

    请注意,输出是一个字符列表,如果您需要将其组合到一个列数据框,您有两个选项。

    1. 使用map_chr -
    tibble(val = map_chr(shopping_list, remove_short_words, 4))
    
    #   val   
    #  <chr> 
    #1 apples
    #2 flour 
    #3 sugar 
    #4 milk  
    
    1. 或者从函数返回一个数据帧/小标题并使用map_df -
    remove_short_words <- function(x, n){
      mypattern <- paste("\\w{", n, ",}", sep="")
      tibble(val = paste(str_extract_all(x, mypattern)[[1]], collapse=' '))
    }
    
    map_df(shopping_list, remove_short_words, 4)
    
    #    val   
    #  <chr> 
    #1 apples
    #2 flour 
    #3 sugar 
    #4 milk  
    

    此外,由于 str_extract_all 是矢量化的,您也可以将其写为 -

    remove_short_words <- function(x, n){
      mypattern <- paste0("\\w{", n, ",}")
      sapply(str_extract_all(x, mypattern), paste, collapse=' ')
    }
    
    tibble(val = remove_short_words(shopping_list, 4))
    
    #    val   
    #  <chr> 
    #1 apples
    #2 flour 
    #3 sugar 
    #4 milk  
    

    【讨论】:

    • 谢谢!我想出了你的想法,但你的解决方案更简洁。 remove_short_words 的最新版本完美替代了我自己编写的内容,但更简洁。
    【解决方案2】:

    我不完全理解你想要做什么,但如果你的 shopping_list 被命名,这个错误就会消失:

    shopping_list2 <- shopping_list %>% 
      set_names(., shopping_list)
    
    map2_df(shopping_list2, 4, function(x,y) remove_short_words(x,y))
    

    请注意,如果n 是常量,则不需要map2 和额外的匿名函数定义。另外,使用map_dfr 或map_dfc(或map2_dfr 或map2_dfc)来指定数据是按行合并还是按列合并。

    map_dfr(shopping_list2, remove_short_words, n = 4)
    

    【讨论】:

    • 谢谢,让我解释一下。我想要一个函数,给定一个字符串向量,从该向量的每个条目中删除所有由少于 3 个字母组成的单词。所需的结果是列向量或小标题(仅 1 列)。返回的对象应该与初始向量的长度相同。
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2016-07-26
    • 2011-11-16
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 2010-09-27
    相关资源
    最近更新 更多