如何在 R 中将字符串向量转换为整数向量?
字符串向量在双引号内包含元素,而整数向量没有任何引号。有时整数值存储在双引号中,因此 R 中这些值的向量被视为字符串向量,但我们需要整数值来执行数学运算。因此,我们可以使用 as.integer 函数将字符串向量转换为整数向量。
示例 1
> x1<-sample(c("1","2","3"),150,replace=TRUE) > x1
输出
[1] "3" "2" "1" "2" "1" "1" "1" "1" "1" "1" "3" "3" "3" "1" "2" "1" "1" "2" [19] "2" "3" "3" "3" "3" "2" "3" "3" "3" "2" "1" "2" "3" "3" "2" "1" "2" "2" [37] "3" "3" "3" "2" "3" "2" "2" "1" "3" "3" "2" "2" "2" "1" "2" "3" "1" "3" [55] "3" "2" "1" "2" "2" "1" "2" "1" "1" "2" "2" "2" "3" "1" "3" "3" "1" "3" [73] "1" "1" "2" "2" "1" "3" "2" "3" "2" "2" "2" "2" "1" "3" "2" "1" "3" "3" [91] "3" "3" "1" "1" "1" "2" "2" "2" "2" "3" "1" "2" "2" "1" "3" "2" "2" "2" [109] "1" "1" "2" "3" "2" "2" "1" "1" "2" "2" "3" "2" "3" "2" "3" "2" "3" "2" [127] "2" "2" "2" "1" "1" "2" "1" "2" "2" "3" "3" "2" "2" "2" "3" "3" "2" "2" [145] "3" "2" "2" "3" "2" "3"
示例
> x1<-as.integer(x1) > x1
输出
[1] 3 2 1 2 1 1 1 1 1 1 3 3 3 1 2 1 1 2 2 3 3 3 3 2 3 3 3 2 1 2 3 3 2 1 2 2 3 [38] 3 3 2 3 2 2 1 3 3 2 2 2 1 2 3 1 3 3 2 1 2 2 1 2 1 1 2 2 2 3 1 3 3 1 3 1 1 [75] 2 2 1 3 2 3 2 2 2 2 1 3 2 1 3 3 3 3 1 1 1 2 2 2 2 3 1 2 2 1 3 2 2 2 1 1 2 [112] 3 2 2 1 1 2 2 3 2 3 2 3 2 3 2 2 2 2 1 1 2 1 2 2 3 3 2 2 2 3 3 2 2 3 2 2 3 [149] 2 3
示例 2
> x2<-sample(c("1","2","3","5","7","11","13","17","19"),160,replace=TRUE) > x2
输出
[1] "19" "1" "19" "1" "13" "7" "11" "1" "13" "3" "19" "7" "3" "11" "7" [16] "3" "5" "1" "3" "11" "3" "2" "3" "5" "7" "7" "19" "7" "11" "7" [31] "7" "5" "17" "11" "7" "17" "2" "5" "5" "5" "5" "1" "13" "13" "5" [46] "19" "1" "13" "3" "3" "3" "19" "7" "7" "2" "3" "5" "1" "2" "5" [61] "3" "17" "11" "1" "13" "1" "1" "19" "17" "2" "17" "17" "11" "17" "13" [76] "2" "5" "2" "1" "17" "5" "5" "1" "13" "2" "13" "2" "2" "13" "19" [91] "3" "2" "1" "2" "11" "11" "13" "17" "19" "11" "19" "11" "1" "5" "19" [106] "7" "13" "19" "13" "11" "17" "11" "19" "2" "7" "19" "5" "17" "17" "5" [121] "1" "1" "7" "5" "11" "5" "7" "17" "13" "5" "1" "17" "13" "3" "1" [136] "17" "5" "5" "1" "2" "19" "11" "11" "7" "1" "5" "7" "13" "3" "2" [151] "2" "5" "17" "2" "7" "19" "19" "19" "7" "3"
示例
> x2<-as.integer(x2) > x2
输出
[1] 19 1 19 1 13 7 11 1 13 3 19 7 3 11 7 3 5 1 3 11 3 2 3 5 7 [26] 7 19 7 11 7 7 5 17 11 7 17 2 5 5 5 5 1 13 13 5 19 1 13 3 3 [51] 3 19 7 7 2 3 5 1 2 5 3 17 11 1 13 1 1 19 17 2 17 17 11 17 13 [76] 2 5 2 1 17 5 5 1 13 2 13 2 2 13 19 3 2 1 2 11 11 13 17 19 11 [101] 19 11 1 5 19 7 13 19 13 11 17 11 19 2 7 19 5 17 17 5 1 1 7 5 11 [126] 5 7 17 13 5 1 17 13 3 1 17 5 5 1 2 19 11 11 7 1 5 7 13 3 2 [151] 2 5 17 2 7 19 19 19 7 3
示例 3
> x3<-sample(c("5","10","15","20"),160,replace=TRUE) > x3
输出
[1] "5" "10" "20" "15" "5" "15" "5" "5" "20" "20" "10" "10" "5" "5" "5" [16] "15" "5" "5" "15" "10" "10" "20" "20" "10" "20" "10" "5" "5" "15" "15" [31] "15" "15" "5" "10" "20" "15" "20" "5" "15" "20" "5" "20" "5" "20" "20" [46] "15" "15" "20" "5" "5" "10" "15" "15" "20" "20" "5" "5" "15" "20" "20" [61] "10" "10" "15" "10" "20" "5" "5" "15" "20" "5" "20" "20" "20" "5" "20" [76] "20" "15" "15" "15" "20" "10" "10" "15" "10" "10" "5" "5" "20" "20" "5" [91] "5" "10" "15" "15" "15" "10" "15" "20" "10" "20" "5" "10" "10" "15" "15" [106] "5" "15" "15" "10" "10" "20" "5" "20" "15" "10" "15" "15" "20" "20" "15" [121] "20" "20" "5" "5" "5" "5" "10" "20" "20" "10" "20" "5" "5" "20" "10" [136] "5" "5" "15" "10" "15" "10" "20" "20" "10" "20" "10" "20" "10" "15" "5" [151] "20" "20" "20" "15" "10" "20" "20" "10" "20" "20"
示例
> x3<-as.integer(x3) > x3
输出
[1] 5 10 20 15 5 15 5 5 20 20 10 10 5 5 5 15 5 5 15 10 10 20 20 10 20 [26] 10 5 5 15 15 15 15 5 10 20 15 20 5 15 20 5 20 5 20 20 15 15 20 5 5 [51] 10 15 15 20 20 5 5 15 20 20 10 10 15 10 20 5 5 15 20 5 20 20 20 5 20 [76] 20 15 15 15 20 10 10 15 10 10 5 5 20 20 5 5 10 15 15 15 10 15 20 10 20 [101] 5 10 10 15 15 5 15 15 10 10 20 5 20 15 10 15 15 20 20 15 20 20 5 5 5 [126] 5 10 20 20 10 20 5 5 20 10 5 5 15 10 15 10 20 20 10 20 10 20 10 15 5 [151] 20 20 20 15 10 20 20 10 20 20
示例 4
> x4<-sample(c("501","515","520","525"),150,replace=TRUE) > x4
输出
[1] "501" "515" "515" "501" "515" "525" "501" "515" "515" "520" "525" "520" [13] "515" "501" "501" "525" "520" "515" "525" "525" "525" "525" "515" "515" [25] "515" "525" "520" "520" "525" "501" "520" "525" "520" "520" "501" "515" [37] "525" "520" "501" "501" "515" "520" "515" "520" "520" "520" "515" "501" [49] "515" "501" "520" "501" "525" "501" "501" "501" "525" "520" "520" "525" [61] "520" "501" "525" "520" "515" "520" "520" "525" "515" "515" "520" "520" [73] "520" "515" "515" "501" "525" "525" "501" "515" "525" "520" "515" "520" [85] "525" "525" "501" "501" "525" "515" "501" "525" "520" "501" "501" "501" [97] "501" "525" "501" "520" "520" "515" "501" "515" "515" "501" "520" "501" [109] "525" "525" "520" "515" "501" "520" "520" "515" "515" "501" "501" "520" [121] "515" "525" "501" "515" "501" "515" "515" "501" "520" "515" "501" "520" [133] "515" "520" "520" "515" "525" "515" "525" "515" "525" "520" "520" "515" [145] "515" "520" "501" "515" "525" "520"
示例
> x4<-as.integer(x4) > x4
输出
[1] 501 515 515 501 515 525 501 515 515 520 525 520 515 501 501 525 520 515 [19] 525 525 525 525 515 515 515 525 520 520 525 501 520 525 520 520 501 515 [37] 525 520 501 501 515 520 515 520 520 520 515 501 515 501 520 501 525 501 [55] 501 501 525 520 520 525 520 501 525 520 515 520 520 525 515 515 520 520 [73] 520 515 515 501 525 525 501 515 525 520 515 520 525 525 501 501 525 515 [91] 501 525 520 501 501 501 501 525 501 520 520 515 501 515 515 501 520 501 [109] 525 525 520 515 501 520 520 515 515 501 501 520 515 525 501 515 501 515 [127] 515 501 520 515 501 520 515 520 520 515 525 515 525 515 525 520 520 515 [145] 515 520 501 515 525 520
广告