tc_data
tc_data

Reputation: 113

Targetting a specific column within a dplyr pipe

I very much enjoy working with the magrittr pipes in R %>% and try to use them as often / efficiently as possible. I quite often need to target specific columns in a pipe chain, for example to change the column type. This results in me having to break the chain / my workflow because I need to target only a specific column instead of my entire dataframe.

Consider the following example:

library(tidyverse)
rm(list = ls())

a <- c(1:20)
b <- rep(c("a", "b"), 10)

df <- data_frame(a, b) %>% 
  rename(info = b) %>% 
  recode(x = df$info, "a" = "x")  #I'd like to target only the df$info column here

This obviously doesn't work, because dplyr doesn't expect me to change the x = argument for a function in a pipe chain.

library(tidyverse)
rm(list = ls())

a <- c(1:20)
b <- rep(c("a", "b"), 10)

df <- data_frame(a, b) %>% 
  rename(info = b)

df$info <- df$info %>%   #this works as expected, but is not as elegant
  recode("a" = "x")

This is how I think it should be done, but I feel that it is not as efficient / elegant as I would like it to be, especially if I plan on chaining more functions together after recoding.

Is there a convenient way around this, so I can tell a command in my pipe chain to target only a specific column?

Upvotes: 3

Views: 3506

Answers (1)

akrun
akrun

Reputation: 887148

We need to place it inside mutate

data_frame(a, b) %>% 
    rename(info = b) %>% 
    mutate(info = recode(info,  a = "x"))
# A tibble: 20 x 2
#       a info 
#   <int> <chr>
# 1     1 x    
# 2     2 b    
# 3     3 x    
# 4     4 b    
# 5     5 x    
# 6     6 b    
# 7     7 x    
# 8     8 b    
# 9     9 x    
#10    10 b    
#11    11 x    
#12    12 b    
#13    13 x    
#14    14 b    
#15    15 x    
#16    16 b    
#17    17 x    
#18    18 b    
#19    19 x    
#20    20 b    

Upvotes: 6

Related Questions