【问题标题】:R create a column by concatenating all other columns in R dataframeR create a column by concatenating all other columns in R dataframe
【发布时间】:2022-12-02 03:28:48
【问题描述】:

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:

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)

【问题讨论】:

    标签: r dataframe concatenation


    【解决方案1】:

    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))
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2021-11-30
      • 2022-12-02
      • 2022-12-28
      • 2022-12-02
      • 2022-12-02
      • 2022-12-27
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多