Home > Software engineering >  Can you use logicals within dplyr's mutate to determine whether a variable is created?
Can you use logicals within dplyr's mutate to determine whether a variable is created?

Time:11-30

I'm trying to create new variables within a pipe if a conditional statement is satisfied.

This code provides an example of what I'm trying to achieve, based on this post: R Conditional evaluation when using the pipe operator %>%.

library(dplyr)
data.frame(x = 1:3) %>% 
  {if (T) mutate(., y = x * 2) else .} %>%  #y is created correctly
  {if (F) mutate(., z = x * 3) else .}  #z is correctly not created since the logical is F
#   x y
# 1 1 2
# 2 2 4
# 3 3 6

I'm wondering if there's a tidier/more concise alternative which uses logicals within mutate() to determine if the variable is created? Something which looks broadly like the next block, but which produces the correct output as per the first block.

data.frame(x = 1:3) %>% 
  mutate(y = if (T) x * 2 else ., 
         z = if (F) x * 3 else .)  #this line results in an additional 'x' column being appended, which is not the desired output
#   x y x
# 1 1 2 1
# 2 2 4 2
# 3 3 6 3  

CodePudding user response:

Columns set to NULL are removed so you can use that in the condition:

library(dplyr)

data.frame(x = 1:3) %>%
  mutate(y = if (T) x * 2 else NULL, 
         z = if (F) x * 3 else NULL)

  x y
1 1 2
2 2 4
3 3 6
  • Related