如何用R中相同向量中的值替换向量中的值?
一个向量中的值替换为同一向量中的值可以借助replace函数来完成。replace函数将使用需要替换的值的索引和需要放置的值的索引,但输出将是向量中的值。
例1
> x1<-rpois(10,1) > x1
输出结果
[1] 3 0 1 0 1 1 1 1 2 1
示例
> replace(x1,c(10),x1[c(1)])
输出结果
[1] 3 0 1 0 1 1 1 1 2 3
例2
> x2<-rpois(150,5) > x2
输出结果
[1] 5 1 4 2 3 4 2 4 5 3 6 2 2 5 4 9 12 3 6 4 7 4 4 4 4 [26] 6 4 6 2 1 6 5 1 6 7 6 8 7 5 0 3 6 2 3 5 2 3 7 8 8 [51] 6 4 5 3 3 4 6 7 4 5 5 2 6 5 6 6 8 2 5 4 2 4 5 7 4 [76] 6 5 4 4 6 12 4 7 3 5 1 4 6 6 4 1 5 4 2 4 3 5 3 7 4 [101] 9 4 4 5 4 4 5 7 4 3 6 7 3 3 3 6 7 2 7 8 4 4 3 7 10 [126] 2 5 8 3 9 5 9 4 6 7 10 2 4 7 5 7 5 5 2 5 3 7 5 6 6
示例
> replace(x2,c(1,5,10),x2[c(101,120,126)])
输出结果
[1] 9 1 4 2 8 4 2 4 5 2 6 2 2 5 4 9 12 3 6 4 7 4 4 4 4 [26] 6 4 6 2 1 6 5 1 6 7 6 8 7 5 0 3 6 2 3 5 2 3 7 8 8 [51] 6 4 5 3 3 4 6 7 4 5 5 2 6 5 6 6 8 2 5 4 2 4 5 7 4 [76] 6 5 4 4 6 12 4 7 3 5 1 4 6 6 4 1 5 4 2 4 3 5 3 7 4 [101] 9 4 4 5 4 4 5 7 4 3 6 7 3 3 3 6 7 2 7 8 4 4 3 7 10 [126] 2 5 8 3 9 5 9 4 6 7 10 2 4 7 5 7 5 5 2 5 3 7 5 6 6
例子3
> x3<-sample(10:99,150,replace=TRUE) > x3
输出结果
[1] 24 84 31 18 83 87 11 99 23 24 84 13 51 63 62 61 71 75 54 24 81 88 71 17 41 [26] 61 10 48 39 85 61 29 55 25 55 58 49 74 19 56 90 18 35 52 48 71 79 73 13 88 [51] 30 90 55 35 33 27 26 97 96 69 14 84 51 56 97 13 33 63 97 80 22 91 57 58 18 [76] 17 87 84 22 28 42 92 70 62 82 41 29 46 69 49 24 45 69 57 46 93 90 25 83 18 [101] 10 67 16 15 62 52 27 19 18 16 88 67 97 38 43 54 93 68 58 41 67 79 37 94 46 [126] 62 39 62 53 64 68 37 89 37 58 50 54 56 47 48 18 19 95 32 78 58 79 66 38 26
示例
> replace(x3,c(1:10),x3[c(141:150)])
输出结果
[1] 18 19 95 32 78 58 79 66 38 26 84 13 51 63 62 61 71 75 54 24 81 88 71 17 41 [26] 61 10 48 39 85 61 29 55 25 55 58 49 74 19 56 90 18 35 52 48 71 79 73 13 88 [51] 30 90 55 35 33 27 26 97 96 69 14 84 51 56 97 13 33 63 97 80 22 91 57 58 18 [76] 17 87 84 22 28 42 92 70 62 82 41 29 46 69 49 24 45 69 57 46 93 90 25 83 18 [101] 10 67 16 15 62 52 27 19 18 16 88 67 97 38 43 54 93 68 58 41 67 79 37 94 46 [126] 62 39 62 53 64 68 37 89 37 58 50 54 56 47 48 18 19 95 32 78 58 79 66 38 26
例子4
> x4<-rnorm(60,5,1) > x4
输出结果
[1] 5.203250 6.155795 5.445043 4.114447 3.708124 5.256640 4.334518 4.088861 [9] 5.223277 4.274886 4.318537 4.642169 3.285436 4.679269 6.872902 4.930993 [17] 5.262924 5.867779 6.184103 5.846095 7.168070 4.421855 5.272398 5.769937 [25] 5.402096 4.330439 4.684108 5.818921 5.895338 4.744801 5.939168 5.274320 [33] 4.002676 5.815148 6.419991 4.509990 5.324781 5.411056 5.667747 4.203748 [41] 4.617290 5.729405 4.588399 3.624062 4.773437 5.203597 4.589123 4.207467 [49] 6.344622 4.266597 5.208964 5.201127 5.362012 6.548678 5.548905 4.312633 [57] 4.169989 5.129504 4.693979 4.875804
示例
> replace(x4,c(1,6:10,21),x4[c(41:47)])
输出结果
[1] 4.617290 6.155795 5.445043 4.114447 3.708124 5.729405 4.588399 3.624062 [9] 4.773437 5.203597 4.318537 4.642169 3.285436 4.679269 6.872902 4.930993 [17] 5.262924 5.867779 6.184103 5.846095 4.589123 4.421855 5.272398 5.769937 [25] 5.402096 4.330439 4.684108 5.818921 5.895338 4.744801 5.939168 5.274320 [33] 4.002676 5.815148 6.419991 4.509990 5.324781 5.411056 5.667747 4.203748 [41] 4.617290 5.729405 4.588399 3.624062 4.773437 5.203597 4.589123 4.207467 [49] 6.344622 4.266597 5.208964 5.201127 5.362012 6.548678 5.548905 4.312633 [57] 4.169989 5.129504 4.693979 4.875804
范例5
> x5<-rexp(75,3.17) > x5
输出结果
[1] 0.6301714207 0.0849452019 0.1441435371 0.4113142011 0.1435970926 [6] 0.4739139703 0.5321786633 0.2116885477 0.4138576102 0.0717753479 [11] 0.2565076697 0.7837426311 0.0141091437 0.0313407699 0.3248291717 [16] 0.1085675881 0.2005076134 0.7493867571 0.5829670716 0.2185303088 [21] 0.2532911149 0.9994490360 0.3659692108 0.0727952412 0.1949328345 [26] 0.5921509557 0.2604765644 0.5033974666 0.2095715146 0.1834696144 [31] 0.4641264025 0.3000919219 0.5795625477 0.1894028427 0.0686694537 [36] 0.1236184164 0.6636295193 1.3764335964 0.5022986936 0.0624637892 [41] 0.0698789223 0.0742836257 0.3393514399 0.0402216820 0.3230930905 [46] 0.2750161774 0.2149539154 0.0166986274 0.4734092166 0.5802175782 [51] 0.1643661184 0.0409281965 0.0625884016 0.0441048258 0.2232389768 [56] 0.0197731224 0.4486469981 0.4061614685 0.9600774628 0.4685170682 [61] 0.2572172289 0.3268631507 0.0072872105 0.4168536975 0.6068680100 [66] 0.4406034999 0.3389223152 0.2223058276 0.6331858868 0.2042540261 [71] 0.0007060446 0.0463169843 0.7482614235 0.0933565609 0.0716075965
示例
> replace(x5,c(71:75),x5[c(1:5)])
输出结果
[1] 0.63017142 0.08494520 0.14414354 0.41131420 0.14359709 0.47391397 [7] 0.53217866 0.21168855 0.41385761 0.07177535 0.25650767 0.78374263 [13] 0.01410914 0.03134077 0.32482917 0.10856759 0.20050761 0.74938676 [19] 0.58296707 0.21853031 0.25329111 0.99944904 0.36596921 0.07279524 [25] 0.19493283 0.59215096 0.26047656 0.50339747 0.20957151 0.18346961 [31] 0.46412640 0.30009192 0.57956255 0.18940284 0.06866945 0.12361842 [37] 0.66362952 1.37643360 0.50229869 0.06246379 0.06987892 0.07428363 [43] 0.33935144 0.04022168 0.32309309 0.27501618 0.21495392 0.01669863 [49] 0.47340922 0.58021758 0.16436612 0.04092820 0.06258840 0.04410483 [55] 0.22323898 0.01977312 0.44864700 0.40616147 0.96007746 0.46851707 [61] 0.25721723 0.32686315 0.00728721 0.41685370 0.60686801 0.44060350 [67] 0.33892232 0.22230583 0.63318589 0.20425403 0.63017142 0.08494520 [73] 0.14414354 0.41131420 0.14359709
范例6
> x6<-runif(60,2,5) > x6
输出结果
[1] 4.339046 3.243330 3.288315 4.397854 2.198220 4.420135 2.458929 4.601673 [9] 3.314666 2.861216 2.210292 2.205213 3.270068 3.283999 4.794161 3.479959 [17] 4.392442 4.051383 3.139061 4.461510 4.322406 3.520107 3.841212 4.172056 [25] 4.311622 4.431082 4.588164 2.485019 3.528105 3.646765 2.677704 2.861034 [33] 3.918297 3.112947 3.891375 3.195276 3.622838 3.959689 4.842700 2.715498 [41] 2.676687 4.768234 3.503961 4.104186 4.747515 4.061376 2.783455 4.346077 [49] 2.364836 3.544960 2.945147 3.345168 3.674116 3.971326 3.167279 2.454910 [57] 4.972975 2.492759 4.899828 4.821637
示例
> replace(x6,c(2,7,11:20,25),x6[c(3:4,41:51)])
输出结果
[1] 4.339046 3.288315 3.288315 4.397854 2.198220 4.420135 4.397854 4.601673 [9] 3.314666 2.861216 2.676687 4.768234 3.503961 4.104186 4.747515 4.061376 [17] 2.783455 4.346077 2.364836 3.544960 4.322406 3.520107 3.841212 4.172056 [25] 2.945147 4.431082 4.588164 2.485019 3.528105 3.646765 2.677704 2.861034 [33] 3.918297 3.112947 3.891375 3.195276 3.622838 3.959689 4.842700 2.715498 [41] 2.676687 4.768234 3.503961 4.104186 4.747515 4.061376 2.783455 4.346077 [49] 2.364836 3.544960 2.945147 3.345168 3.674116 3.971326 3.167279 2.454910 [57] 4.972975 2.492759 4.899828 4.821637