1

I have a character matrix (3 x 2) that looks as follows:

“A” “1”
“B” “1”
“C” “2”

Both columns are text strings. I would like to convert this matrix to a new matrix (4 x 2) that looks as follows:

“A” 1
“B” 1
“C” 1
“C” 1

Essentially, because the initial row 3 has the number two at the end, it means that string C must be repeated twice in the new matrix. I would also like to convert column 2 from text to numeric.

What would be the quickest and most efficient way to do this? I have a dataset that is over 2m lines long, so I need something that is robust but also quick to "expand" the matrix accordingly.

Thanks!

Aveshen Pillay
  • 431
  • 3
  • 13
  • 1
    If you convert the matrix to dataframe you can use any of the answers here - https://stackoverflow.com/questions/2894775/repeat-each-row-of-data-frame-the-number-of-times-specified-in-a-column – Ronak Shah Aug 06 '20 at 04:23

2 Answers2

4

One option would be tidyr::uncount:

within(tidyr::uncount(as.data.frame(m), as.numeric(V2)), V2 <- 1)
#>     V1 V2
#> 1    a  1
#> 2    b  1
#> 3    c  1
#> 3.1  c  1

Data

m <- matrix(c("a", "b", "c", "1", "1", "2"), ncol = 2)
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
2

We need to convert the second column to numeric and with rep replicate the values of first column based on the values in the second column and create col2 as 1 in data.frame call

data.frame(col1 = rep(m1[,1], as.integer(m1[,2])), col2 = 1)
akrun
  • 874,273
  • 37
  • 540
  • 662