Home > Enterprise >  R create a column by concatenating all other columns in R dataframe
R create a column by concatenating all other columns in R dataframe

Time:12-02

I have a data frame with say 20 columns, I would like to create another column that concatenates all other columns, is there a concise way to write the code without expicity writing out each column's column name?

For example my data call model_spec looks like:

enter image description here

this is my current solution: it worked but the problem is that it's very hard=coded and what if I have 100 variables? is there way to achieve this automatically and concatenate all columns without ugly-looking code of explicitly writing out each column name? Thanks in advance! any advice or point to method/function is appreciated!

want = model_spec %>% mutate(
  model_formula = paste0('y ~' , var1 ,' ', var2, ' ',var3,' ',var4,' ',var5,
                         ' ',var6,' ',var7, ' ',var8,' ',var9,' ',var10,
                         ' ',var11,' ',var12,' ',var13,' ',var14,' ',var15,' ',var16,' ', 
                         var17,' ',var18, ' ', var19, ' ', var20))
  ) %>% select(model_id, model_formula)

CodePudding user response:

We could do something like this:

library(dplyr)
library(tidyr)

df %>% 
  unite("new_col", var1:var6, na.rm=TRUE, sep = "   ") %>% 
  mutate(new_col = paste0("y ~ ", new_col))
  model_id                 new_col
1        1 y ~ gdp   ur   dpi   ir
2        1      y ~ ur   ltv   dti

data:

df <- structure(list(model_id = c(1L, 1L), var1 = c("gdp", NA), var2 = c("ur", 
"ur"), var3 = c(NA, "ltv"), var4 = c("dpi", NA), var5 = c(NA, 
"dti"), var6 = c("ir", NA)), class = "data.frame", row.names = c(NA, 
-2L))
  • Related