Skip to content

Instantly share code, notes, and snippets.

@nk773
Created October 7, 2016 19:48
Show Gist options
  • Select an option

  • Save nk773/8ea51168ce54c19ca1e7861f47338c3d to your computer and use it in GitHub Desktop.

Select an option

Save nk773/8ea51168ce54c19ca1e7861f47338c3d to your computer and use it in GitHub Desktop.
Concatenate multiple columns into a single column
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