Dplyr: use chaining to pass variables

I am new to dplyr and cannot figure out how to control variables passing through the chain ( %>% ). A simple example: the str_sub function takes three arguments - the first is passed through %>% , but how can I get the last two?

 library(stringr) library(dplyr) df <- data.frame(V1 = c("ABBEDHH", "DEFGH", "EFGF", "EEFD"), V2=c(4, 2, 1, 1), V3=c(5, 2, 2, 1), stringsAsFactors=FALSE) 

In the R base, I could do:

 with(df, str_sub(V1, V2, V3)) 

and get:

 ## [1] "ED" "E" "EF" "E" 

How to tie this up? - I tried:

 df %>% str_sub(V1, V2, V3) # Here V3 is unused arg since V1 is treated as 2nd arg df %>% select(V1) %>% str_sub(V2, V3) # Here V2 and V3 are not recognized 
+5
source share
2 answers

You can do the following:

 library(dplyr) library(stringr) library(lazyeval) df %>% mutate(new = str_sub(V1, V2, V3)) # V1 V2 V3 new #1 ABBEDHH 4 5 ED #2 DEFGH 2 2 E #3 EFGF 1 2 EF #4 EEFD 1 1 E 

Note that dplyr is executed to work with data.frame s, so the input and output must be data.frames, not atomic vectors.

+5
source

You can also do:

 df %>% with(str_sub(V1, V2, V3)) 

since you need a vector. But now we are back in nesting ground.

+1
source

Source: https://habr.com/ru/post/1206091/


All Articles