如何为R向量中的范围设置特定值?
假设我们有一个向量,其中包含从1到100的数百个值,并且想要设置大于5且小于96到5的值,则可以使用ifelse函数来完成。例如,如果将这样的向量命名为x,则命令将如下所示-
ifelse(x>5 & x<96,0,x)
例1
> x1<-sample(0:9,200,replace=TRUE) > x1输出结果
[1] 2 4 1 6 7 4 0 1 6 4 0 7 1 3 3 1 4 6 7 7 0 2 7 3 9 4 4 8 6 3 3 5 4 5 6 5 6 [38] 2 8 6 9 7 8 5 6 4 4 6 0 9 4 5 1 4 4 5 9 5 8 1 7 8 3 5 9 4 3 6 3 1 7 0 2 9 [75] 8 8 8 2 0 0 2 6 1 0 1 6 3 6 8 6 6 7 5 3 2 1 0 6 9 1 0 3 7 9 5 2 1 6 7 9 7 [112] 0 8 2 3 2 5 6 5 9 4 3 0 1 7 0 8 3 6 7 9 6 7 0 9 6 0 7 2 0 5 2 4 6 3 5 0 6 [149] 1 9 0 4 9 6 3 3 2 9 0 3 2 6 8 5 4 1 8 2 1 5 2 4 5 5 1 4 0 9 9 1 0 2 0 1 3 [186] 8 5 8 9 0 9 6 9 2 5 4 7 2 8 0
将0设置为3到7之间的值-
示例
> ifelse(x1>3 & x1<7,0,x1)输出结果
[1] 2 0 1 0 7 0 0 1 0 0 0 7 1 3 3 1 0 0 7 7 0 2 7 3 9 0 0 8 0 3 3 0 0 0 0 0 0 [38] 2 8 0 9 7 8 0 0 0 0 0 0 9 0 0 1 0 0 0 9 0 8 1 7 8 3 0 9 0 3 0 3 1 7 0 2 9 [75] 8 8 8 2 0 0 2 0 1 0 1 0 3 0 8 0 0 7 0 3 2 1 0 0 9 1 0 3 7 9 0 2 1 0 7 9 7 [112] 0 8 2 3 2 0 0 0 9 0 3 0 1 7 0 8 3 0 7 9 0 7 0 9 0 0 7 2 0 0 2 0 0 3 0 0 0 [149] 1 9 0 0 9 0 3 3 2 9 0 3 2 0 8 0 0 1 8 2 1 0 2 0 0 0 1 0 0 9 9 1 0 2 0 1 3 [186] 8 0 8 9 0 9 0 9 2 0 0 7 2 8 0
例2
> x2<-sample(1:20,200,replace=TRUE) > x2输出结果
[1] 2 15 12 20 7 11 6 20 4 14 20 7 12 5 10 19 19 1 8 2 13 19 16 7 13 [26] 18 7 4 11 3 17 8 19 10 17 10 13 14 12 17 16 13 13 19 15 6 7 15 20 6 [51] 18 6 1 6 3 13 6 10 13 9 12 19 15 9 20 11 8 14 19 18 4 19 7 16 8 [76] 10 10 8 16 5 11 5 15 11 18 13 16 6 20 3 15 8 16 3 9 7 2 19 3 12 [101] 1 15 12 9 13 11 3 3 13 16 1 10 9 20 7 3 3 17 8 10 18 16 15 4 9 [126] 13 10 11 20 8 1 3 5 9 5 3 2 14 1 2 19 8 11 1 8 18 2 17 19 16 [151] 13 17 11 6 15 15 14 12 16 14 17 11 17 19 4 3 12 4 11 4 18 11 1 10 2 [176] 14 11 2 16 14 9 12 8 4 3 17 4 20 15 5 11 8 14 10 17 2 12 20 2 18
将5设置为5到15之间的值-
示例
> ifelse(x2>5 & x1<15,5,x2)输出结果
[1] 2 5 5 5 5 5 5 5 4 5 5 5 5 5 5 5 5 1 5 2 5 5 5 5 5 5 5 4 5 3 5 5 5 5 5 5 5 [38] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 5 3 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 5 5 5 [75] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 3 5 5 5 3 5 5 2 5 3 5 1 5 5 5 5 5 3 3 5 5 1 [112] 5 5 5 5 3 3 5 5 5 5 5 5 4 5 5 5 5 5 5 1 3 5 5 5 3 2 5 1 2 5 5 5 1 5 5 2 5 [149] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 5 4 5 4 5 5 1 5 2 5 5 2 5 5 5 5 5 4 3 [186] 5 4 5 5 5 5 5 5 5 5 2 5 5 2 5
例子3
> x3<-sample(11:99,200,replace=TRUE) > x3输出结果
[1] 40 65 33 84 86 99 61 26 16 21 15 59 78 68 77 90 54 53 34 55 94 96 50 17 52 [26] 90 14 81 34 94 70 32 43 11 75 12 25 85 54 32 33 61 48 12 52 13 25 34 59 76 [51] 90 63 33 55 34 85 22 61 53 26 50 35 34 32 24 19 49 50 65 77 69 69 54 67 25 [76] 40 88 63 37 49 29 14 43 63 20 59 79 96 74 45 83 65 43 83 60 69 84 47 97 12 [101] 82 24 50 87 12 94 17 54 86 72 52 28 89 41 64 54 82 33 40 65 32 74 20 73 99 [126] 22 84 69 23 92 61 63 31 51 71 11 30 44 84 65 27 17 27 71 30 23 44 87 28 70 [151] 42 66 46 18 26 82 90 64 27 77 15 73 87 12 38 77 56 64 94 26 34 71 48 32 27 [176] 77 81 70 58 53 56 48 88 47 65 34 80 29 70 80 46 83 91 38 31 75 81 85 73 30
将60设置为41到80之间的值-
示例
> ifelse(x3>41 & x3<80,60,x3)输出结果
[1] 40 60 33 84 86 99 60 26 16 21 15 60 60 60 60 90 60 60 34 60 94 96 60 17 60 [26] 90 14 81 34 94 60 32 60 11 60 12 25 85 60 32 33 60 60 12 60 13 25 34 60 60 [51] 90 60 33 60 34 85 22 60 60 26 60 35 34 32 24 19 60 60 60 60 60 60 60 60 25 [76] 40 88 60 37 60 29 14 60 60 20 60 60 96 60 60 83 60 60 83 60 60 84 60 97 12 [101] 82 24 60 87 12 94 17 60 86 60 60 28 89 41 60 60 82 33 40 60 32 60 20 60 99 [126] 22 84 60 23 92 60 60 31 60 60 11 30 60 84 60 27 17 27 60 30 23 60 87 28 60 [151] 60 60 60 18 26 82 90 60 27 60 15 60 87 12 38 60 60 60 94 26 34 60 60 32 27 [176] 60 81 60 60 60 60 60 88 60 60 34 80 29 60 80 60 83 91 38 31 60 81 85 60 30
例子4
> x4<-sample(100:1000,200) > x4输出结果
[1] 533 751 598 902 190 544 483 739 184 823 402 995 241 317 491 453 885 644 [19] 181 148 232 167 992 761 519 923 530 999 105 291 572 326 760 155 573 864 [37] 310 517 425 793 862 663 540 348 816 524 937 226 958 506 835 375 407 736 [55] 843 633 997 141 580 875 145 532 698 808 108 452 725 497 656 300 172 896 [73] 498 877 605 409 152 175 769 275 654 289 723 331 474 818 886 820 797 321 [91] 209 853 138 979 134 737 872 494 388 832 895 767 991 437 295 770 806 652 [109] 821 201 845 511 783 522 423 553 762 884 588 122 248 582 541 143 287 277 [127] 475 587 218 914 552 247 204 664 187 236 268 246 441 358 891 320 890 649 [145] 928 520 514 280 345 165 882 757 482 486 600 935 403 266 684 329 819 893 [163] 594 149 203 738 646 746 352 551 163 918 925 873 802 554 795 496 398 694 [181] 741 779 293 860 457 921 794 257 912 242 982 111 507 286 107 499 752 641 [199] 274 503
将999设置为300到800之间的值-
示例
> ifelse(x4>300 & x4<800,999,x4)输出结果
[1] 999 999 999 902 190 999 999 999 184 823 999 995 241 999 999 999 885 999 [19] 181 148 232 167 992 999 999 923 999 999 105 291 999 999 999 155 999 864 [37] 999 999 999 999 862 999 999 999 816 999 937 226 958 999 835 999 999 999 [55] 843 999 997 141 999 875 145 999 999 808 108 999 999 999 999 300 172 896 [73] 999 877 999 999 152 175 999 275 999 289 999 999 999 818 886 820 999 999 [91] 209 853 138 979 134 999 872 999 999 832 895 999 991 999 295 999 806 999 [109] 821 201 845 999 999 999 999 999 999 884 999 122 248 999 999 143 287 277 [127] 999 999 218 914 999 247 204 999 187 236 268 246 999 999 891 999 890 999 [145] 928 999 999 280 999 165 882 999 999 999 999 935 999 266 999 999 819 893 [163] 999 149 203 999 999 999 999 999 163 918 925 873 802 999 999 999 999 999 [181] 999 999 293 860 999 921 999 257 912 242 982 111 999 286 107 999 999 999 [199] 274 999
范例5
> x5<-sample(rnorm(10),100,replace=TRUE) > x5输出结果
[1] -0.57111665 0.07053565 1.20533459 0.07053565 -0.57111665 -0.71958043 [7] -0.65966153 0.89605057 0.07053565 0.07053565 -1.16714167 0.29694819 [13] 0.89605057 0.89605057 -0.65966153 -0.57111665 -0.57111665 -1.16714167 [19] -1.16714167 -0.71958043 0.07053565 1.20533459 0.07053565 0.89605057 [25] 0.29694819 -0.57111665 -0.57111665 0.89605057 0.07053565 -0.71958043 [31] -0.65966153 -1.16714167 0.71313347 0.07053565 -1.16714167 -0.57111665 [37] 0.71313347 -0.65966153 -1.16714167 0.07053565 1.20533459 0.71313347 [43] -1.16714167 1.20533459 -0.71958043 -0.71958043 1.20533459 -0.65966153 [49] -0.71958043 -0.71958043 0.71313347 0.64869502 0.07053565 0.89605057 [55] -0.65966153 1.20533459 -0.57111665 -0.57111665 0.89605057 0.64869502 [61] -0.65966153 0.64869502 -0.65966153 -0.57111665 -0.65966153 0.29694819 [67] 0.71313347 -0.71958043 1.20533459 0.07053565 0.29694819 0.29694819 [73] -0.57111665 -0.71958043 0.71313347 -0.71958043 -0.65966153 0.07053565 [79] 0.07053565 -0.57111665 0.29694819 -0.57111665 -0.57111665 0.64869502 [85] 1.20533459 0.89605057 0.71313347 0.71313347 0.29694819 0.07053565 [91] 1.20533459 0.07053565 -1.16714167 0.89605057 0.71313347 -0.71958043 [97] -0.65966153 -0.57111665 0.89605057 -0.71958043
将NA设置为0到1之间的值-
示例
> ifelse(x5>0 & x5<1,NA,x5)输出结果
[1] -0.5711167 NA 1.2053346 NA -0.5711167 -0.7195804 [7] -0.6596615 NA NA NA -1.1671417 NA [13] NA NA -0.6596615 -0.5711167 -0.5711167 -1.1671417 [19] -1.1671417 -0.7195804 NA 1.2053346 NA NA [25] NA -0.5711167 -0.5711167 NA NA -0.7195804 [31] -0.6596615 -1.1671417 NA NA -1.1671417 -0.5711167 [37] NA -0.6596615 -1.1671417 NA 1.2053346 NA [43] -1.1671417 1.2053346 -0.7195804 -0.7195804 1.2053346 -0.6596615 [49] -0.7195804 -0.7195804 NA NA NA NA [55] -0.6596615 1.2053346 -0.5711167 -0.5711167 NA NA [61] -0.6596615 NA -0.6596615 -0.5711167 -0.6596615 NA [67] NA -0.7195804 1.2053346 NA NA NA [73] -0.5711167 -0.7195804 NA -0.7195804 -0.6596615 NA [79] NA -0.5711167 NA -0.5711167 -0.5711167 NA [85] 1.2053346 NA NA NA NA NA [91] 1.2053346 NA -1.1671417 NA NA -0.7195804 [97] -0.6596615 -0.5711167 NA -0.7195804