ben890
ben890

Reputation: 1133

Add columns to dataframe set to initial value based on list dplyr

If I have a vector let's call it x:

x <- c('a', 'b', 'c', 'd', 'e')

and I let's say I also have a dataframe df, what is the dplyr way to create columns for each value in the vector and intialize them at asome value (eg. 0). I know this would be simple in base R as I could do the following:

df[,x] <- 0

However I was curious about the dplyr way. Thanks!

Upvotes: 2

Views: 705

Answers (1)

IRTFM
IRTFM

Reputation: 263382

The [<- function can be used in dplyr type calls, although I don't think it is any more elegant. I'm assigning to 1 rather than 0.

features <- 
structure(list(f1 = structure(1:2, .Label = c("blue", "geeen"
), class = "factor"), f2 = c(0, 0), a = c(0, 0)), row.names = c(NA, 
-2L), class = "data.frame")

features %>% `[<-`(TRUE, c("a","b","c"), 1)
     f1 f2 a b c
1  blue  0 1 1 1
2 geeen  0 1 1 1

I did discover that numeric assignments to factor columns get handled a bit differently with the dplyr coding. The base [<- assignment will coerce to numeric, while the combined use of "%>%" with "[<-" witll result in 's of factor class:

str( features %>% `[<-`(TRUE, "f1", 0) )
#--------------
'data.frame':   2 obs. of  3 variables:
 $ f1: Factor w/ 2 levels "blue","geeen": NA NA
 $ f2: num  0 0
 $ a : num  0 0
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = c(0, 0)) :
  invalid factor level, NA generated

Upvotes: 1

Related Questions