r - Create a formula from data frame column names -


i'm trying find way format numeric function, splitting variables , adding math operator in between them. like

y <- data.frame(1:5, 2:6, 3:7,4:8) names(y) <- c('a', 'b', 'c', 'd') 

and turn formula looks this:

~a+b+c+d 

i tried:

x <-c(y) z <- paste('a', 'b', 'c', 'd', sep='+') as.numeric(as.character(z)) 

it doesn't work, , i'm pretty sure it's because of "character" separator i'm using. how go doing this?

> paste(names(y), collapse='+') [1] "a+b+c+d" 

the parameter using sep. should using collapse parameter. hope helps.


Comments