5

I'm new to dplyr and cannot figure out how to control the variables to pass through a chaining (%>%) command. Simple example: the str_sub function takes three arguments - the first is passed on 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 base R I could do:

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

and get:

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

How to chain this ? - 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
jbaums
  • 27,115
  • 5
  • 79
  • 119
user3375672
  • 3,728
  • 9
  • 41
  • 70

2 Answers2

5

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 made for working with data.frames, so input and output should be data.frames, not atomic vectors.

talat
  • 68,970
  • 21
  • 126
  • 157
  • Perfect! For my need I would then add df %>% mutate(new = str_sub(V1, V2, V3)) %>% select(new) – user3375672 Nov 03 '14 at 13:55
  • @user3375672, yes, you can do that, but note that that works because the result is still a data.frame (with only 1 column), not an atomic vector. – talat Nov 03 '14 at 14:13
  • 1
    @user3375672 you could use the slightly akward: `df %>% mutate(new = str_sub(V1, V2, V3)) %>% \`[[\`("new")` – Tyler Rinker Nov 03 '14 at 17:29
  • @TylerRinker, good point. Although, iIf they really wanted a vector as output, my personal choice would simply be `with(df, str_sub(V1, V2, V3))`, as shown in the question. – talat Nov 03 '14 at 17:47
1

One could also do:

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

as you want a vector anyway. But now we're back in nested land.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519