如何用 R 向量中的 2 替换小于 2 的向量值?
如果我们有一个向量,其中包含小于、等于和大于 2 的值,并且值 2 是阈值。如果此阈值定义为较低的值,并且我们想用 2 替换小于 2 的值,则可以使用 pmax 函数。例如,对于向量 x,它将被完成为 pmax(x,2)。
示例 1
x1<−rpois(10,4)输出结果x1
[1] 2 6 6 3 4 5 5 1 5 4
示例
pmax(x1,2)输出结果
[1] 2 6 6 3 4 5 5 2 5 4
例2
x2<−rpois(150,2)输出结果x2
[1] 1 2 0 2 1 5 1 5 1 2 2 1 1 2 3 5 2 0 1 2 1 5 2 2 3 2 2 2 3 2 3 1 2 2 2 2 0[38] 2 3 2 3 3 2 2 1 5 3 2 0 1 2 3 3 3 4 1 1 4 4 5 4 1 0 1 6 3 2 1 2 1 1 3 4 1
[75] 1 0 0 2 3 5 2 2 1 2 4 0 3 1 2 1 2 3 1 1 3 2 8 3 1 2 1 3 1 2 0 2 0 2 0 2 2
[112] 1 1 2 1 3 2 2 4 3 1 2 3 1 3 3 1 1 2 1 2 1 1 1 3 1 3 0 2 3 3 1 1 2 1 4 1 3
[149] 2 1
示例
pmax(x2,2)输出结果
[1] 2 2 2 2 2 5 2 5 2 2 2 2 2 2 3 5 2 2 2 2 2 5 2 2 3 2 2 2 3 2 3 2 2 2 2 2 2[38] 2 3 2 3 3 2 2 2 5 3 2 2 2 2 3 3 3 4 2 2 4 4 5 4 2 2 2 6 3 2 2 2 2 2 3 4 2
[75] 2 2 2 2 3 5 2 2 2 2 4 2 3 2 2 2 2 3 2 2 3 2 8 3 2 2 2 3 2 2 2 2 2 2 2 2 2
[112] 2 2 2 2 3 2 2 4 3 2 2 3 2 3 3 2 2 2 2 2 2 2 2 3 2 3 2 2 3 3 2 2 2 2 4 2 3
[149] 2 2
例3
x3<−rpois(200,1)输出结果x3
[1] 1 3 1 0 0 0 2 3 1 0 1 0 0 0 1 0 1 0 4 0 0 1 2 3 2 0 1 0 1 0 0 1 2 2 2 1 1输出结果[38] 1 1 1 0 1 3 1 3 2 0 1 0 0 0 2 2 1 2 1 0 1 2 3 1 1 3 0 2 1 2 1 0 1 1 2 1 1
[75] 0 3 0 4 1 2 2 1 0 1 1 1 0 0 1 0 0 0 0 0 0 0 2 3 0 0 1 2 0 0 0 1 1 0 1 1 1
[112] 1 1 0 0 3 2 2 0 2 1 2 1 1 3 1 1 2 2 0 1 2 1 1 0 0 1 2 2 1 2 2 2 0 1 2 1 0
[149] 1 2 0 4 0 2 1 0 0 0 2 1 1 0 2 1 0 2 2 4 1 2 1 0 2 1 2 0 0 2 1 0 2 1 1 0 1
[186] 1 1 2 1 1 2 1 3 1 0 4 2 4 0 0
pmax(x3,2)输出结果
[1] 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 4 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2[38] 2 2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 2 2 2 2 2
[75] 2 3 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2
[112] 2 2 2 2 3 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[149] 2 2 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[186] 2 2 2 2 2 2 2 3 2 2 4 2 4 2 2
例4
x4<−rnorm(80,2,0.5)输出结果x4
[1] 1.713307 2.340440 2.203869 2.143247 1.747553 1.843345 1.364853 1.762649[9] 2.015612 2.221302 1.923381 1.382785 2.183772 1.420598 1.685303 1.804460
[17] 1.538778 2.559414 1.883867 1.799137 2.394044 1.289769 2.052858 1.681691
[25] 2.533696 2.250131 1.211450 1.458721 2.609805 1.438549 3.190229 2.225142
[33] 2.636581 2.098613 2.575711 1.536273 2.311053 1.893953 1.720211 1.593571
[41] 1.088694 2.217778 1.808863 2.076323 1.834031 2.243204 2.297566 1.607128
[49] 2.333699 2.813765 1.474731 1.371792 1.533625 3.476363 2.021140 2.032780
[57] 1.926966 1.660207 2.013881 1.902092 1.590748 1.821373 1.394533 2.171105
[65] 2.518966 1.919101 2.520436 1.884282 2.269978 2.266681 1.377183 1.679837
[73] 2.377076 2.010263 1.850560 1.557361 2.802616 1.454833 1.761261 1.836139
示例
pmax(x4,2)输出结果
[1] 2.000000 2.340440 2.203869 2.143247 2.000000 2.000000 2.000000 2.000000[9] 2.015612 2.221302 2.000000 2.000000 2.183772 2.000000 2.000000 2.000000
[17] 2.000000 2.559414 2.000000 2.000000 2.394044 2.000000 2.052858 2.000000
[25] 2.533696 2.250131 2.000000 2.000000 2.609805 2.000000 3.190229 2.225142
[33] 2.636581 2.098613 2.575711 2.000000 2.311053 2.000000 2.000000 2.000000
[41] 2.000000 2.217778 2.000000 2.076323 2.000000 2.243204 2.297566 2.000000
[49] 2.333699 2.813765 2.000000 2.000000 2.000000 3.476363 2.021140 2.032780
[57] 2.000000 2.000000 2.013881 2.000000 2.000000 2.000000 2.000000 2.171105
[65] 2.518966 2.000000 2.520436 2.000000 2.269978 2.266681 2.000000 2.000000
[73] 2.377076 2.010263 2.000000 2.000000 2.802616 2.000000 2.000000 2.000000
例5
x5<−runif(50,1,5)输出结果x5
[1] 1.099467 1.041738 3.489129 2.929942 3.622750 1.677293 3.403260 4.074329输出结果[9] 2.896510 4.404937 4.918171 2.149239 3.153985 1.039821 1.504592 4.232741
[17] 1.193438 4.619456 3.124406 1.574481 1.773586 4.639662 2.017406 4.307287
[25] 4.524485 2.372469 4.061478 1.340272 3.935372 3.539032 1.478809 2.701063
[33] 4.747307 3.420348 3.601933 2.090273 1.342867 4.937700 4.686878 2.548134
[41] 1.457458 4.470995 1.426985 4.897287 3.682281 2.791846 3.429494 4.285380
[49] 1.572455 1.731519
pmax(x5,2)输出结果
[1] 2.000000 2.000000 3.489129 2.929942 3.622750 2.000000 3.403260 4.074329[9] 2.896510 4.404937 4.918171 2.149239 3.153985 2.000000 2.000000 4.232741
[17] 2.000000 4.619456 3.124406 2.000000 2.000000 4.639662 2.017406 4.307287
[25] 4.524485 2.372469 4.061478 2.000000 3.935372 3.539032 2.000000 2.701063
[33] 4.747307 3.420348 3.601933 2.090273 2.000000 4.937700 4.686878 2.548134
[41] 2.000000 4.470995 2.000000 4.897287 3.682281 2.791846 3.429494 4.285380
[49] 2.000000 2.000000
以上是 如何用 R 向量中的 2 替换小于 2 的向量值? 的全部内容, 来源链接: utcz.com/z/311470.html