Created
October 7, 2016 19:48
-
-
Save nk773/8ea51168ce54c19ca1e7861f47338c3d to your computer and use it in GitHub Desktop.
Concatenate multiple columns into a single column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| concatenate<-function(dataset1, colrng, sep=" ") { | |
| rng <- which(names(dataset1)%in%colrng) | |
| if (length(rng)<2) { | |
| print(paste("Insufficient columns for concatenation", length(rng), rng)) | |
| return(dataset1) | |
| } | |
| df<-dataset1[,-rng] | |
| df3<-apply(dataset1[,rng],1, paste, collapse=sep) | |
| df3<-data.frame(df3) | |
| names(df3)<-c("merged") | |
| return (cbind(df,df3)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment