如何在dplyr管道中使用stringr

ell*_*iot 3 r stringr dplyr

我在尝试编辑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 = '')
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

Duc*_*mas 6

您需要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 "
Run Code Online (Sandbox Code Playgroud)

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