6

I am having trouble with this code which attempts to edit some strings in a dplyr pipe. Here is some data that throws the following error. Any ideas?

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 = '')

Gives me this error: Error in str_trunc(., string = .$name, width = 10, side = "right", ellipsis = ". . . ") : unused argument (.).

Thanks.

elliot
  • 1,844
  • 16
  • 45
  • I don't think you need to provide the ```.``` when you use the ```%>%``` pipe. It's assumed that you're carrying out the function on the data frame that's being piped in. – divibisan Mar 22 '18 at 20:55
  • Some people like to supply `.` anyway to make the code more explicit. I think the argument is that when you're teaching new people how dplyr works, they are always presented with a consistent interface to base R instead of an arg being "dropped". – DuckPyjamas Mar 23 '18 at 19:12

3 Answers3

9

You need to mutate or mutate_at/if/all to change the contents of the column.

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 "

I use mutate_at here out of personal preference. Note that the mutating column is automatically passed as the first argument. If you want to put it somewhere else in the function call, refer to it as ..

DuckPyjamas
  • 1,539
  • 13
  • 17
0

There is no data parameter in str_trunc, so you need to feed it the string. You can use

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 = '')
smanski
  • 541
  • 2
  • 7
0

if you want to add/update column from existing one, please use mutate function.

you can't use the stringr function directly in the pipe.

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 "

the mutate(blah blah) is equivalent to followings

> 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 
BJK
  • 153
  • 5