【问题标题】:How to use `stringr` in `dplyr` pipe如何在`dplyr`管道中使用`stringr`
【发布时间】:2018-09-01 10:59:19
【问题描述】:

我在尝试编辑dplyr 管道中的某些字符串的代码时遇到问题。这是一些引发以下错误的数据。有什么想法吗?

data_frame(id = 1:5,
           name = c('this and it pretty long is a',
                    'name is a',
                    'so and so and so and so  and so',
                    'this is a',
                    'this is a variabel name')) 
%>% 

str_trunc(.,
   string = .$name,
   width = 10,
   side='right',
   ellipsis = '')

给我这个错误:Error in str_trunc(., string = .$name, width = 10, side = "right", ellipsis = ". . . ") : unused argument (.)

谢谢。

【问题讨论】:

  • 当您使用%>% 管道时,我认为您不需要提供.。假设您正在通过管道输入的数据框执行该功能。
  • 有些人喜欢提供. 以使代码更明确。我认为争论的焦点是,当您教新人 dplyr 如何工作时,他们总是会看到与基本 R 一致的接口,而不是“丢弃” arg。

标签: r dplyr stringr


【解决方案1】:

您需要mutatemutate_at/if/all 更改列的内容。

data_frame(id = 1:5,
       name = c('this and it pretty long is a',
                'name is a',
                'so and so and so and so  and so',
                'this is a',
                'this is a variabel name')) %>% 
mutate_at("name", str_trunc, width = 10, side='right', ellipsis = '')

# A tibble: 5 x 2
     id name        
  <int> <chr>       
1     1 this and i  
2     2 name is a   
3     3 "so and so "
4     4 this is a   
5     5 "this is a "

出于个人喜好,我在这里使用mutate_at。请注意,变异列会自动作为第一个参数传递。如果您想将其放在函数调用中的其他位置,请将其称为.

【讨论】:

    【解决方案2】:

    str_trunc 中没有data 参数,因此您需要将string 提供给它。你可以使用

    data_frame(id = 1:5,
               name = c('this and it pretty long is a',
                        'name is a',
                        'so and so and so and so  and so',
                        'this is a',
                        'this is a variabel name'))$name %>% 
      str_trunc(width = 10,
                side='right',
                ellipsis = '')
    

    【讨论】:

      【解决方案3】:

      如果您想从现有列中添加/更新列,请使用mutate 函数。

      你不能直接在管道中使用 stringr 函数。

      data_frame(id = 1:5,
                 name = c('this and it pretty long is a',
                          'name is a',
                          'so and so and so and so  and so',
                          'this is a',
                          'this is a variabel name'))  %>% 
                 mutate(name=str_trunc(name,width=10,side='right',ellipsis=''))
      ## # A tibble: 5 x 2
      ##      id name        
      ##   <int> <chr>       
      ## 1     1 this and i  
      ## 2     2 name is a   
      ## 3     3 "so and so "
      ## 4     4 this is a   
      ## 5     5 "this is a "
      

      mutate(blah blah) 等价于下面的

      > df<-data_frame(id = 1:5,
              name = c('this and it pretty long is a',
                       'name is a',
                       'so and so and so and so  and so',
                       'this is a',
                       'this is a variabel name'))
      
      > df
      ## # A tibble: 5 x 2
      ##      id name                           
      ##   <int> <chr>                          
      ## 1     1 this and it pretty long is a   
      ## 2     2 name is a                      
      ## 3     3 so and so and so and so  and so
      ## 4     4 this is a                      
      ## 5     5 this is a variabel name        
      > df$name<-str_trunc(df$name,width=10,side='right',ellipsis='')
      > df  
      ## # A tibble: 5 x 2
      ##      id name        
      ##   <int> <chr>       
      ## 1     1 this and i  
      ## 2     2 name is a   
      ## 3     3 "so and so "
      ## 4     4 this is a   
      ## 5     5 "this is a 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-25
        • 2020-04-30
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多