Method to copy down rows R

老子叫甜甜 提交于 2019-12-22 07:28:11

问题


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.


回答1:


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

?



来源:https://stackoverflow.com/questions/14145819/method-to-copy-down-rows-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!