如何将r中的向量增加一个固定值并生成每次迭代的直方图

时间:2018-02-08 08:14:54

标签: r vector iteration

我希望将向量中的每个值迭代1,直到达到设定值并将每次迭代保存在向量中,并且进一步的迭代不包括超过设定值的值。因此,例如说设定值是3.考虑该矢量,A< -c(1,1,2)。那么期望的结果应该是:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title><!-- 
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> -->
</head>

<body>
    <span id="counter" onclick="test(this.id)">1,234,567.15</span>
</body>
<script>
function test(event) {
    console.log(arguments[0]);
    console.log(event);//counter,because you pass id 'counter' ,you didn't pass event
}
</script>

</html>

然后我想将每一行存储在一个向量中,以便绘制直方图 所以每个矢量结果都包括原始矢量。

Outcome:
1 1 2
2 2 3
3 3 

2 个答案:

答案 0 :(得分:1)

   D1 D2 D3 ..... D10
P1  1  .  .
P2  2  6  22
P3  3  8  45
P4  4  15  64
P5  5  17   .
P6  6  15  353
P7  7  11  12
P8  8  4   45
P9  9  96  23
P10 10  12 12

答案 1 :(得分:0)

您可以使用while循环:

funfun=function(vec,max){
  y=list()
  i=1
  while(length(vec)!=0){
    y[[i]]=vec
    vec=vec+1
    vec=`attributes<-`(na.omit(replace(vec,vec>max,NA)),NULL)
    i=i+1
  }
  y
}

funfun(c(1,1,2),3)
[[1]]
[1] 1 1 2

[[2]]
[1] 2 2 3

[[3]]
[1] 3 3

你现在可以做到

sapply(funfun(c(1,1,2),3),hist)