How to convert a string vector into an integer vector in R?

R ProgrammingServer Side ProgrammingProgramming

A string vector contains element inside double-quotes and an integer vector does not have any quotes. Sometimes integer values are stored in double-quotes hence the vector of these values is treated as a string vector in R but we need the integer values to perform mathematical operations. Therefore, we can use as.integer function to convert the string vector into an integer vector.

Example1

Live Demo

> x1<-sample(c("1","2","3"),150,replace=TRUE)
> x1

Output

[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"

Example

> x1<-as.integer(x1)
> x1

Output

[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

Example2

Live Demo

> x2<-sample(c("1","2","3","5","7","11","13","17","19"),160,replace=TRUE)
> x2

Output

[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"

Example

> x2<-as.integer(x2)
> x2

Output

[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

Example3

Live Demo

> x3<-sample(c("5","10","15","20"),160,replace=TRUE)
> x3

Output

[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"

Example

> x3<-as.integer(x3)
> x3

Output

[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

Example4

Live Demo

> x4<-sample(c("501","515","520","525"),150,replace=TRUE)
> x4

Output

[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"

Example

> x4<-as.integer(x4)
> x4

Output

[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
raja
Published on 23-Nov-2020 10:33:40
Advertisements