Concatenate columns and add them to beginning of Data Frame

前端 未结 3 656
终归单人心
终归单人心 2021-01-01 04:52

Noob here to R. Trying to figure something out. I need to build a function that adds a new column to the beginning of a dataset. This new column is a concatenation of the va

3条回答
  •  青春惊慌失措
    2021-01-01 04:57

    This should do the trick

    addPrimaryKey <-function(df, cols){
    
       q<-apply(df[,cols], 1, function(x) paste(x, collapse=""))
    
       df<-cbind(q, df)
    
       return(df)
    
    }
    

    Just add in some conditional logic for your nulls

提交回复
热议问题