首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在“`dplyr`”管中使用“`stringr`”

如何在“`dplyr`”管中使用“`stringr`”
EN

Stack Overflow用户
提问于 2018-03-22 20:53:30
回答 3查看 5.6K关注 0票数 5

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

代码语言:javascript
复制
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 (.)

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-22 20:58:09

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

代码语言:javascript
复制
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是出于个人喜好。请注意,变异列将作为第一个参数自动传递。如果您想将它放在函数调用的其他地方,请将其称为.

票数 7
EN

Stack Overflow用户

发布于 2018-03-22 21:00:33

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

代码语言:javascript
复制
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 = '')
票数 0
EN

Stack Overflow用户

发布于 2018-03-22 21:06:54

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

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

代码语言:javascript
复制
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 "

变异(诸如此类)相当于以下内容:

代码语言:javascript
复制
> 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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49438265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档