Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to copy down rows R

Tags:

copy

r

row

Suppose we have a dataframe or matrix with one column specifying an integer value N as below (col 5). Is there a vector approach to repopulate the object such that each row gets copied N times?

> y
            [,1]       [,2]        [,3]        [,4] [,5]
[1,] -0.02738267  0.5170621 -0.01644855  0.48830663    1
[2,] -0.30076544  1.8136359  0.02319640 -1.59649330    2
[3,]  1.73447245  0.4043638 -0.29112385 -0.25102988    3
[4,]  0.01025271 -0.4908636  0.80857300  0.08137033    4

The result would be as follows.

[1,] -0.02738267  0.5170621 -0.01644855  0.48830663    1
[2,] -0.30076544  1.8136359  0.02319640 -1.59649330    2    
[2,] -0.30076544  1.8136359  0.02319640 -1.59649330    2
[3,]  1.73447245  0.4043638 -0.29112385 -0.25102988    3
[3,]  1.73447245  0.4043638 -0.29112385 -0.25102988    3
[3,]  1.73447245  0.4043638 -0.29112385 -0.25102988    3
[4,]  0.01025271 -0.4908636  0.80857300  0.08137033    4
[4,]  0.01025271 -0.4908636  0.80857300  0.08137033    4
[4,]  0.01025271 -0.4908636  0.80857300  0.08137033    4
[4,]  0.01025271 -0.4908636  0.80857300  0.08137033    4

Another question would be how to jitter the newly populated rows, such that there is not compute overlap of the newly copied data.

like image 564
pat Avatar asked Jan 03 '13 19:01

pat


1 Answers

Some made-up data:

y <- cbind(matrix(runif(16), 4, 4), 1:4)

Just do:

z <- y[rep(seq_len(nrow(y)), y[,5]), ]
#            [,1]       [,2]       [,3]      [,4] [,5]
#  [1,] 0.5256007 0.07467979 0.95189484 0.2887943    1
#  [2,] 0.3083967 0.03518523 0.08380005 0.9168161    2
#  [3,] 0.3083967 0.03518523 0.08380005 0.9168161    2
#  [4,] 0.8549639 0.79452728 0.22483537 0.4452553    3
#  [5,] 0.8549639 0.79452728 0.22483537 0.4452553    3
#  [6,] 0.8549639 0.79452728 0.22483537 0.4452553    3
#  [7,] 0.5453508 0.47633523 0.51522514 0.3936340    4
#  [8,] 0.5453508 0.47633523 0.51522514 0.3936340    4
#  [9,] 0.5453508 0.47633523 0.51522514 0.3936340    4
# [10,] 0.5453508 0.47633523 0.51522514 0.3936340    4

And I am not sure what you mean by "jitter", but maybe

z <- z + runif(z) / 1000

?

like image 75
flodel Avatar answered Sep 21 '22 05:09

flodel