15

Is there a "by row" operation in R to convert each row in a vector like this to strings?

> d= cbind("Data", c("2", "73"))
> d
     [,1]   [,2]
[1,] "Data" "2" 
[2,] "Data" "73"

What I want is to get strings like

     [,1]
[1,] "Data 2"
[2,] "Data 73"

Is there an easy way to join items by row?

gakera
  • 3,589
  • 4
  • 30
  • 36

3 Answers3

27

Yes, there is. It is called "apply" ;-)

apply(d,1,paste,collapse=" ")
[1] "Data 2"  "Data 73"
# convert to matrix using as.matrix to get exactly your solution

See ?apply and ?paste

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • blarg, of course, the apply, duh :( But the paste, collapse= " " part I would never have figured out, thanks :) – gakera Nov 18 '10 at 10:20
  • @gakera: Figuring out the `collapse=" "` part is pretty easy if you read the documentation `?paste`. – Joshua Ulrich Nov 18 '10 at 15:45
  • The help doesn't include any examples of collapse=" " usage, at least not in my version. But from the text it's kinda clear what it does, once you see it in all the noise. – gakera Nov 18 '10 at 15:50
  • @gakera: tss tss tss... it's not noise, it's very valuable information actually. At a certain point you'll be more than happy with the "noise". – Joris Meys Nov 18 '10 at 15:54
  • Hehe, I like how quick people are to defend the R Help, it's like it's everybody's baby. Very good, I appreciate all the help I can get. And yes, SO has a very good reputation of being a good place to get a quick answer, and delivers usually without fail. I'm not ashamed of being a little lazy when it comes to this kind of stuff. I leave it to you to define what "little" means, I'm sure you'll enjoy it. – gakera Nov 18 '10 at 16:03
5

A general way to do it without resorting to ?apply:

do.call(paste, as.data.frame(d))
[1] "Data 2"  "Data 73"

Where as.data.frame is used to avoid subscripts.

Edit:

do.call is a function which takes another function as first argument, and a list as second argument. It is often used to send lists of arguments to functions (in our case, the columns of d to paste()). We send d as a data.frame (a type of list) for this trick to work.

Beginning_Math
  • 121
  • 2
  • 2
  • That's true. The efficiency gain would be greater if the data was already a data.frame, but the `apply()` solution is far faster for vectors. The advantage in this one is that one need not limit oneself to a single data type when applying paste, but there are also a lot of disadvantages: for example, pasting by rows instead of columns only requires a number change in the `apply()` solution, whereas this one requires major code changes. Overall, this solution is only for my typical use cases, where I did an index with `expand.grid()` and concatenate these with `do.call(paste)` – Beginning_Math Mar 18 '16 at 13:37
4

After a quick glace at ?paste, it's clear that apply isn't needed for the example given. It would be handy if there are several columns though.

> paste(d[,1],d[,2])
[1] "Data 2"  "Data 73"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • But I need a general way that works without specifying the rows explicitly. I'm sorry I wasn't clear enough about that, but the apply method works very well for what I need to do. – gakera Nov 18 '10 at 15:58