Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Collapse By Row

Tags:

r

paste

I need to collapse my data by row.

seq = structure(c("h", "d", "s", "s", "f", "k", "s", "s", "f", "d", 
"d", "d", "l", "l", "d", "d"), .Dim = c(4L, 4L), .Dimnames = list(
NULL, c("act1.055", "act1.056", "act1.057", "act1.058")))

My data look like this :

     act1.055 act1.056 act1.057 act1.058
[1,] "h"      "f"      "f"      "l"     
[2,] "d"      "k"      "d"      "l"     
[3,] "s"      "s"      "d"      "d"     
[4,] "s"      "s"      "d"      "d" 

If I do

paste(seq, collapse = "")
[1] "hdssfkssfdddlldd"

which is not what I want.

What I need is

hffl
dkdl 
ssdd 
ssdd

In a matrix if possible.

like image 886
giac Avatar asked Oct 25 '25 11:10

giac


1 Answers

matrix(apply(seq, 1, paste, collapse=''), ncol=1)
#     [,1]  
#[1,] "hffl"
#[2,] "dkdl"
#[3,] "ssdd"
#[4,] "ssdd"
like image 112
Pierre L Avatar answered Oct 27 '25 02:10

Pierre L