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.
来源:https://stackoverflow.com/questions/33335636/dplyr-pipes-how-to-change-the-original-dataframe