How to find the position of minimum value in a vector that contains integers as strings in R?

R ProgrammingServer Side ProgrammingProgramming

If integer vector is read as strings and we want to find the position of the minimum value in that vector then we need to use as.numeric along with the vector to read it as a numeric vector then use which function for finding the position of the minimum value. For example, if we have a vector x that contains first ten integers as strings then to find the position of the minimum we can use which(as.numeric(x)==min(as.numeric(x))).

Example1

x1<−c("1","2","3","4")
which(as.numeric(x1)==min(as.numeric(x1)))
[1] 1

Example2

 Live Demo

x2<−sample(c("18","22","13","14","25","8","17"),120,replace=TRUE)
x2
[1] "17" "25" "25" "8" "8" "25" "22" "18" "14" "17" "17" "13" "13" "18" "17"
[16] "25" "8" "14" "17" "25" "22" "13" "18" "18" "22" "18" "25" "18" "17" "22"
[31] "18" "18" "14" "17" "8" "22" "17" "25" "13" "14" "18" "18" "8" "17" "14"
[46] "14" "8" "22" "8" "14" "18" "25" "14" "18" "18" "18" "14" "25" "18" "13"
[61] "18" "22" "25" "13" "22" "14" "14" "17" "8" "17" "18" "18" "22" "17" "18"
[76] "13" "18" "13" "25" "18" "8" "22" "8" "17" "13" "22" "22" "14" "25" "8"
[91] "18" "17" "13" "8" "14" "17" "13" "13" "14" "14" "13" "14" "8" "17" "8"
[106] "17" "22" "25" "25" "8" "8" "22" "13" "18" "18" "25" "8" "8" "17" "17"
which(as.numeric(x2)==min(as.numeric(x2)))
[1] 4 5 17 35 43 47 49 69 81 83 90 94 103 105 110 111 117 118

Example3

 Live Demo

x3<−sample(c("118","322","413","214","125","418","317","247","258","320"),120,replace=TRUE)
x3
[1] "320" "317" "118" "418" "413" "413" "214" "247" "247" "317" "258" "258"
[13] "247" "317" "247" "214" "125" "125" "413" "247" "320" "320" "258" "214"
[25] "322" "125" "320" "118" "125" "317" "322" "322" "320" "317" "118" "322"
[37] "320" "247" "418" "214" "258" "418" "322" "317" "258" "118" "247" "118"
[49] "320" "258" "247" "320" "258" "118" "418" "413" "214" "247" "418" "413"
[61] "214" "320" "320" "118" "214" "413" "418" "418" "317" "320" "118" "247"
[73] "247" "214" "125" "317" "214" "247" "322" "413" "125" "322" "125" "258"
[85] "418" "320" "317" "258" "214" "320" "118" "413" "317" "418" "118" "320"
[97] "247" "317" "317" "413" "247" "320" "413" "317" "413" "320" "320" "258"
[109] "322" "413" "320" "214" "247" "214" "125" "320" "317" "118" "125" "322"
which(as.numeric(x3)==min(as.numeric(x3)))
[1] 3 28 35 46 48 54 64 71 91 95 118

Example4

 Live Demo

x4<−sample(c("518","309","213","235","325","328","317","247","358","420","299","500"),120,replace=TRUE)
x4
[1] "420" "328" "247" "500" "500" "358" "317" "500" "317" "235" "358" "317"
[13] "317" "247" "235" "247" "299" "358" "518" "213" "325" "299" "317" "309"
[25] "235" "299" "317" "518" "247" "309" "328" "317" "500" "358" "328" "299"
[37] "309" "358" "235" "420" "325" "328" "299" "500" "299" "317" "328" "309"
[49] "299" "420" "247" "235" "358" "235" "358" "500" "518" "328" "325" "420"
[61] "309" "213" "328" "420" "213" "213" "518" "317" "299" "213" "235" "309"
[73] "420" "299" "358" "213" "325" "325" "420" "235" "358" "500" "500" "328"
[85] "247" "213" "309" "420" "213" "518" "328" "420" "420" "500" "213" "358"
[97] "518" "213" "358" "247" "235" "299" "247" "500" "309" "317" "420" "420"
[109] "317" "213" "500" "420" "309" "500" "309" "309" "213" "328" "213" "518"
which(as.numeric(x4)==min(as.numeric(x4)))
[1] 20 62 65 66 70 76 86 89 95 98 110 117 119

Example5

 Live Demo

x5<−sample(c("5112","2542","3241","3211","4125","2568","3742","2784","4269","3627"),120,replace=TRUE)
x5
[1] "3211" "4125" "2784" "5112" "3211" "3742" "4269" "2542" "3742" "2568"
[11] "2568" "2568" "2784" "2784" "3742" "3742" "2542" "5112" "2542" "5112"
[21] "5112" "3211" "3627" "4125" "3742" "5112" "2542" "2568" "2542" "3742"
[31] "3211" "2542" "2568" "3241" "4125" "3211" "3742" "2568" "3742" "4125"
[41] "3241" "3211" "3241" "4269" "4269" "2784" "2784" "3627" "2568" "2542"
[51] "2542" "2784" "2542" "2568" "2542" "4125" "2542" "2568" "3627" "5112"
[61] "2784" "2568" "4125" "2568" "4125" "3211" "2568" "2784" "3627" "2784"
[71] "3742" "5112" "2568" "2542" "4125" "5112" "4125" "3211" "4269" "3742"
[81] "3627" "2568" "2784" "5112" "2784" "3211" "3627" "2542" "3627" "4269"
[91] "3742" "2542" "2542" "4269" "3211" "2784" "3742" "2568" "3627" "3742"
[101] "3742" "4125" "2784" "2784" "4125" "3742" "3627" "3627" "3241" "4125"
[111] "4125" "2784" "5112" "2542" "3627" "5112" "3241" "3742" "2568" "2784"
which(as.numeric(x5)==min(as.numeric(x5)))
[1] 8 17 19 27 29 32 50 51 53 55 57 74 88 92 93 114
raja
Published on 06-Nov-2020 18:30:12
Advertisements