Search code examples
rdataframepastesapply

How to refer to a column name in a function


I have a df. If 'info' appears in a row, I would like a function to take the column-name and and paste it together with the information from the cell with a '=' between them. I have tried the function below, which works, except that it does not refer to the columnname from the right column

df <- data.frame('criteria1' = c('info','1', 'info', '', 'info'), "criteria2" = c('y','3', '', 'info', ''), "criteria3" = c('y','7', '', 'info', 'info'))

df[] <- sapply(df, function(x) ifelse(x == 'info', paste(colnames(df)[x], ' = ', x),''))

My expected output is something like this (it does not matter if the columnnames are deleted, it is just the info from the cells that are important)

df_exp <- data.frame('criteria1' = c('criteria1= info','', 'criteria1=info', '', 'criteria1 =info'), "criteria2" = c('','', '', 'criteria2 = info', ''), "criteria3" = c('','', '', 'criteria3 = info', 'criteria3 = info'))

Solution

  • I'd go for the column numbers, either 1:ncol(df) (which is faster) or seq(df). I use the former.

    df <- sapply(1:ncol(df), function(x) 
      ifelse(df[[x]] == 'info', paste(colnames(df)[x], ' = ', df[[x]]),''))
    df
    #                      [,1]                 [,2]                 [,3]                
    # [1,] "criteria1  =  info" ""                   ""                  
    # [2,] ""                   ""                   ""                  
    # [3,] "criteria1  =  info" ""                   ""                  
    # [4,] ""                   "criteria2  =  info" "criteria3  =  info"
    # [5,] "criteria1  =  info" ""                   "criteria3  =  info"
    

    Another nice way using stack/unstack:

    r <- grep("info", tmp$values)
    tmp <- stack(df)
    tmp[r, 1] <- apply(tmp[r, 2:1], 1, paste, collapse="=")
    tmp[-r, 1] <- ""  ## in case you want non-"info" cells cleared
    df <- unstack(tmp)
    df
    #        criteria1      criteria2      criteria3
    # 1 criteria1=info                              
    # 2                                             
    # 3 criteria1=info                              
    # 4                criteria2=info criteria3=info
    # 5 criteria1=info                criteria3=info