# paste unknown number of columns

#### trinker

##### ggplot2orBust
I have founf several close but not quite answers to this problem that I know is probably very simple.

I want to take a list of equal length vectors (could be numeric or character) and paste them together using "." as the separator. the number of columns being passed is unknown.

Here are my attempts:

Code:
x <- CO2[, 1:3]
y <- CO2[, 1:4]

paste(x, sep=".")
paste(y, sep=".")

paste2 <- function(x) paste(x, sep=".") #attempt with do.call
do.call("paste2", y)

#### Dason

I don't quite get what you want. Can you provide a brief sample input and desired output?

#### trinker

##### ggplot2orBust
The x and the y were just showung the number of columns was unknown.

I want the outcome for x to be:
Code:
"Qn1.Quebec.nonchilled" "Qn1.Quebec.nonchilled" "Qn1.Quebec.nonchilled" ...
and for y the output would have the same plus another dot and the 4th column. Note the length of the new vector will be = to length of the combined vectors.

Maybe this is an mapply solution but I'm in the car now without R so can't check.

#### Lazar

##### Phineas Packard
Code:
dat<-data.frame(x=rnorm(10), y=letters[1:10])

cat(as.character(dat$x),sep=".", "\n") cat(as.character(dat$y),sep=".",  "\n")
is this what you mean.

OR
Code:
f1<-function(x) cat(as.character(x),sep=".",  "\n")
f2<- colwise(f1)

f2(dat)

Last edited:

#### trinker

##### ggplot2orBust
@Lazar I can't check this yet but the use of cat doesn't't seem right. I want to combine predictors (row by row) to make a final vector that is a character string of the predictors pasted together.

This may also be q job for reduce.

I would have thought I could have just used paste though.

One more problem is that if any calls in the paste are NA then it needs to return NA.

#### Dason

Do either of these do what you want?

Code:
x <- CO2[, 1:3]
y <- CO2[, 1:4]

apply(x, 1, paste, collapse = ".")
apply(x, 1, function(x){if(any(is.na(x))){NA}else{paste(x, collapse = ".")}})

#### trinker

##### ggplot2orBust
@Dason I was just about to write that I got the apply and was going to work on NA. when apply came to me I was like "You bone head". That saves me from figuring out the NA problem. Much appreciated.

#### Dason

No problem. Sorry about the horribly formatted code. When I write code here and don't actually check it within R I tend to just write gross one-liners.

#### bryangoodrich

##### Probably A Mammal
Just looking at the OP, you say you want to use a list of vectors but then refer to columns. Lists don't have columns! You don't provide an example of the input or the output! Bad form!

#### trinker

##### ggplot2orBust
I was doing it from my phone. Geesh cut a guy a break BG And technically a data frame is a list of equal length vectors. So CO2 is an example of the input. I took care of the output when dason asked. There lies your criticisms in a pile of rubbish. I hypothesize this post is just a red herring and you're playing both the bots and raptors.

Is your Monday that hard that you're picking on poor trinker now? And besides my teeth are 4 inches long. I ask is it wise to pick fights with raptors. That makes me wonder why would you not be afraid? Unless....That's it!!!!!!!

We're all worried about bots and raptors meanwhile who's looking out for Wepwawet the one who ushers the dead into hell. My gosh bots and raptors may take your life but Wepwawet takes your soul. Beware TSers he's a snake in the grass.

Last edited:

#### bryangoodrich

##### Probably A Mammal
Tall tales.

Btw, you could have did the paste thing quite easily from do.call if you understand that you need to supply it a list object that contains all the parameters you want to use. This includes the "sep" parameter.

Code:
set.seed(100)
df <- data.frame(A = runif(10, 0, 10), B = runif(10, 0, 10), C = runif(10, 0, 10))
y <- as.list(df)  # make it a list

#### bryangoodrich

##### Probably A Mammal
Well the y$sep was just to show that you don't need a wrapper. What you're passing as a list in do.call just is the parameters to the function, as a list. No different than if I had Code: f <- function(x, y, d) { ... } args <- list() args$x <- 1
args$y <- 2 args$d <- c("bob", "foo")
do.call('f', args)
f(x = 1, y = 2, d = c('bob', 'foo'))  # equivalent
What you didn't do in your 'paste2' was to pass the actual things being pasted together. You only passed a separator. That is where the "..." comes in. You pass one thing: 'x'.

Code:
paste2  <- function(x)              paste(x,   sep = ".")  # yours
mypaste <- function(..., sep = ".") paste(..., sep = sep)  # mine
So I can do something like mypaste("bob", "joe", "frank") whereas you do paste2(x = "joe") and that's about it. At best, you can pass 'x' as a vector of things to be pasted together and then set collapse = ".", but it makes more sense to use the "..." function parameter that takes unspecified inputs and passes them to wherever you use "..." in your function, which in my case was to the paste function itself, and on mine you can also still alter the separator, where yours is hardcoded as-is, forever!