dplyr管道 - 如何更改原始数据框 [英] dplyr pipes - How to change the original dataframe
问题描述
当我不使用管道时,我可以使用这个命令来更改原来的daframe
df< -slice( df,-c(1:3))%>%#删除顶部3行
df< -select(df,-c(Col1,Col50,Col51))#删除特定列
如何用管道做这个?我尝试过这个,但切片
和选择
函数不会更改原始数据帧。
df%>%
片(-c(1:3))%>%
选择(-c(Col1,Col50,Col51))
我想更改原始df。
你可以通过使用一个成语(例如 df< - df %>%...
或 df%>%... - > DF
。但是您也可以使用 magrittr
复合赋值运算符<$ c $,以避免冗余(即说明 df
两次) c>%<>%在管道的开头。
从 magrittr
vignette:
复合分配管道运算符
%<%$ / code>可以用作链中的第一个管道。效果将是将流水线的结果分配给左侧的对象,而不是像往常一样返回结果。
所以用你的代码,我们可以做
code> library(magrittr)##来自你的dplyr安装
df%<>%slice( - (1:3))%>%select(-c(Col1,Col50,Col51 )
这个管道 df
并更新 df
作为结果。
更新:在评论中您注意到问题设置列名称。幸运的是, magrittr
提供了在管道中设置属性的功能。尝试以下。
df%<>%
set_colnames(sprintf(Col%d,1 :ncol(。)))%>%
slice( - (1:3))%>%
select(-c(Col1,Col50,Col51))
<注意,由于我们有一个数据框,我们也可以使用 setNames()
(stats)或 set_names()
(magrittr)代替 set_colnames()
。
感谢Steven Beaupre从小插曲中添加笔记。
When I don't use a pipe, I can change the original daframe using this command
df<-slice(df,-c(1:3))%>% # delete top 3 rows
df<-select(df,-c(Col1,Col50,Col51)) # delete specific columns
How would one do this with a pipe? I tried this but the slice
and select
functions don't change the original dataframe.
df%>%
slice(-c(1:3))%>%
select(-c(Col1,Col50,Col51))
I'd like to change the original df.
解决方案 You can definitely do the assignment by using an idiom such as df <- df %>% ...
or df %>% ... -> df
. But you could also avoid redundancy (i.e., stating df
twice) by using the magrittr
compound assignment operator %<>%
at the beginning of the pipe.
From the magrittr
vignette:
The compound assignment pipe operator %<>%
can be used as the first pipe in a chain. The effect will be that the result of the pipeline is assigned to the left-hand side object, rather than returning the result as usual.
So with your code, we can do
library(magrittr) ## came with your dplyr install
df %<>% slice(-(1:3)) %>% select(-c(Col1, Col50, Col51))
This pipes df
into the expression and updates df
as the result.
Update: In the comments you note an issue setting the column names. Fortunately magrittr
has provided functions for setting attributes in a pipe. Try the following.
df %<>%
set_colnames(sprintf("Col%d", 1:ncol(.))) %>%
slice(-(1:3)) %>%
select(-c(Col1,Col50,Col51))
Note that since we have a data frame, we can also use setNames()
(stats) or set_names()
(magrittr) in place of set_colnames()
.
Thanks to Steven Beaupre for adding the note from the vignette.
这篇关于dplyr管道 - 如何更改原始数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!