复制行R的方法 [英] Method to copy down rows R
本文介绍了复制行R的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个向量方法来重新填充对象,使每行被复制N次?
> 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
结果如下。
[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
解决方案一些化妆数据:
y< - cbind (matrix(runif(16),4,4),1:4)
/ p>
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
# ] 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
我不知道你的意思jitter,但也许
z < - z + runif(z)/ 1000
?
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.
解决方案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
?
这篇关于复制行R的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文