**TRinker's R Blog » R**, and kindly contributed to R-bloggers)

**This is Part I of a multi part blog on the paste2 functionâ€¦**

I recently generated a new paste function that takes an unspecified list of equal length variables (a column) or multiple columns of a data frame and pastes them together. First let me thank Dason of Talk Stats for his help in this post that led to the creation of the `paste2`

function. First let me convince you of the need for a paste2 function by showing you where the original paste falls short. Then Iâ€™ll introduce to the function and some basics of what it can do. In Part II of this paste2 blog series Iâ€™ll show you a few practical applications Iâ€™ve already encountered.

The main idea behind this function is the need to pass an unknown number of columns from a data frame or list and paste them together to generate an uber column that contains all the information of the original columns. You may say well I think paste already does that. Not in itâ€™s home grown state it doesnâ€™t. Whatâ€™s that prove it? OK. Try the following:

paste(CO2[, 1:3], sep=".") #1 paste(CO2[, 1:3], collapse=".") #2 paste(CO2[,1], CO2[, 2], CO2[, 3], sep=".") #3 paste(list(CO2[,1], CO2[, 2], CO2[, 3]), sep=".") #4

What do you get? Well the third use of paste is the only one that results in pasting the columns together. Why? Because we specified the columns being passed to paste and paste is our friend. If we try a sneak attack with an index of columns paste becomes scared and returns gobly gook. If you try to be nice and give paste a list of columns he again gives gobly gook. So whatâ€™s the need for pasting an unknown number of columns (either as an indexed data frame or as a list) together? Often in functions the number of columns passed to paste canâ€™t be specified in advance, hence out problem (Iâ€™ll show you more of those specific applications in Part II).

paste2 <- function(multi.columns, sep=".", handle.na=TRUE, trim=TRUE){ if (trim) multi.columns <- lapply(multi.columns, function(x) { gsub("^\\s+|\\s+$", "", x) } ) if (!is.data.frame(multi.columns) & is.list(multi.columns)) { multi.columns <- do.call('cbind', multi.columns) } m <- if(handle.na){ apply(multi.columns, 1, function(x){ if (any(is.na(x))){ NA } else { paste(x, collapse = sep) } } ) } else { apply(multi.columns, 1, paste, collapse = sep) } names(m) <- NULL return(m) }

**Now letâ€™s see it in action:**

paste2(CO2[, 1:3], sep=".") paste2(CO2[, 1:3], sep=":") paste2(list(CO2[,1], CO2[, 2], CO2[, 3])) #shoot we can paste the whole data set if we want paste2(CO2) paste2(mtcars)

In Part II weâ€™ll explore some practical uses of this new function!

Click HERE for a link to a .txt version of paste2

**leave a comment**for the author, please follow the link and comment on their blog:

**TRinker's R Blog » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...