0

I want to convert each row into a character consisting of the values in each column.

head(sequence_mat, 5)
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    0    0    0    0    0    0
 [2,]    0    1    0    0    0    0
 [3,]    0    0    1    0    0    0
 [4,]    0    0    0    1    0    0
 [5,]    0    0    0    0    1    0

For example, sequence_mat[1,] = 0 0 0 0 0 0 and I want it to become "0-0-0-0-0-0".

I tried as.character(sequence_mat[1,]) but the output is not what I want, i.e. [1] "0" "0" "0" "0" "0" "0"

azal
  • 1,210
  • 6
  • 23
  • 43

1 Answers1

1

You can do this with

apply(sequence_mat, 1, paste, collapse = "-")

This runs through the array (matrix or dataframe) row-wise, pasting the elements together, with - separating them.

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32