C. Braun
C. Braun

Reputation: 5211

Create new columns based on vector in dplyr

With dplyr it is easy to create a new column using mutate:

df <- data.frame(v1 = 1:3, v2 = c('a','b','c'))
> mutate(df, newcol = NA)
  v1 v2 newcol
1  1  a     NA
2  2  b     NA
3  3  c     NA

We can also create multiple new columns with a vector using mutate_at (shown here):

> cnames <- c('newcol1', 'newcol2', 'newcol3')
> mutate_at(df, cnames, funs(log(v1)))
  v1 v2   newcol1   newcol2   newcol3
1  1  a 0.0000000 0.0000000 0.0000000
2  2  b 0.6931472 0.6931472 0.6931472
3  3  c 1.0986123 1.0986123 1.0986123

Is there a simple way to initialize these new columns as NA using dplyr?

For example, mutate_at(df, cnames, funs(v1 * NA)) gives the desired result, but that seems indirect. What I would like is something along the lines of:

mutate_at(df, cnames, funs(. = NA)) # Error: Can't create call to non-callable object

where we don't need to know the names of any other columns.

(I know this is simply solved with df[ , cnames] <- NA, but I'm looking for a solution using dplyr functions)


EDIT:

Using later versions of dplyr the example becomes:

mutate_at(df, all_of(cnames), funs(log(v1)))

Upvotes: 3

Views: 3123

Answers (1)

markus
markus

Reputation: 26373

You could do this.

library(dplyr)
df %>% 
 `is.na<-`(cnames)
#  v1 v2 newcol1 newcol2 newcol3
#1  1  a      NA      NA      NA
#2  2  b      NA      NA      NA
#3  3  c      NA      NA      NA

I hope one %>% is dplyr enough. ;)

Upvotes: 5

Related Questions