How to find minimum value in a numerical vector which is written as a character vector in R?


To find the minimum value of a numeric vector we can directly use min function but if the values are read with double-inverted commas then min function does not work. In this case, we will have to use as.numeric with the vector name so that it can be converted to numeric form before finding the minimum value. For example, if we have a character vector that contains 1, 2, 3, and 4 then the minimum can be found as min(as.numeric(x)).

Example1

 Live Demo

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

Example2

 Live Demo

x2<−sample(c("18","22","13","14","25","8","17"),120,replace=TRUE)
x2
[1] "18" "22" "22" "25" "14" "17" "14" "14" "18" "22" "25" "25" "22" "14" "17"
[16] "17" "13" "14" "18" "18" "8" "25" "25" "25" "25" "14" "22" "8" "17" "22"
[31] "18" "18" "14" "8" "14" "14" "17" "18" "14" "17" "22" "18" "25" "18" "22"
[46] "14" "18" "22" "14" "25" "13" "8" "17" "13" "22" "8" "13" "17" "8" "13"
[61] "25" "8" "17" "14" "22" "8" "17" "14" "13" "17" "8" "18" "22" "8" "8"
[76] "13" "13" "22" "25" "17" "25" "22" "18" "22" "25" "14" "18" "17" "17" "13"
[91] "25" "13" "25" "22" "8" "18" "14" "8" "13" "8" "18" "13" "22" "22" "25"
[106] "8" "13" "25" "25" "8" "13" "8" "18" "17" "25" "25" "25" "14" "25" "8"
min(as.numeric(x2))
[1] 8

Example3

 Live Demo

x3<−sample(c("118","322","413","214","125","418","317","247","258","320"),120,replace=TRUE)
x3
[1] "247" "418" "317" "320" "317" "258" "258" "247" "418" "258" "247" "322"
[13] "418" "214" "125" "320" "322" "320" "320" "418" "258" "418" "317" "258"
[25] "118" "118" "317" "125" "247" "320" "247" "125" "214" "320" "317" "125"
[37] "118" "418" "118" "418" "418" "125" "418" "125" "118" "247" "258" "322"
[49] "125" "125" "214" "317" "214" "413" "317" "214" "118" "322" "418" "317"
[61] "258" "214" "258" "320" "125" "247" "317" "258" "214" "322" "214" "418"
[73] "317" "418" "320" "125" "125" "118" "125" "247" "125" "258" "320" "247"
[85] "214" "247" "258" "320" "214" "214" "320" "418" "322" "322" "317" "317"
[97] "258" "214" "320" "320" "214" "125" "258" "413" "317" "118" "413" "320"
[109] "258" "125" "247" "247" "214" "247" "214" "247" "214" "413" "418" "258"
min(as.numeric(x3))
[1] 118

Example4

x4<−sample(c("518","309","213","235","325","328","317","247","358","420","299""500"),120,replace=TRUE)
x4
[1] "328" "299" "358" "299" "518" "247" "299" "299" "500" "317" "420" "325"
[13] "317" "328" "420" "317" "325" "247" "235" "247" "247" "518" "299" "518"
[25] "299" "317" "317" "325" "420" "325" "325" "328" "358" "213" "309" "213"
[37] "518" "420" "309" "518" "299" "420" "247" "235" "325" "518" "420" "317"
[49] "325" "328" "309" "358" "500" "299" "299" "213" "235" "420" "213" "317"
[61] "247" "247" "299" "420" "325" "317" "317" "358" "235" "420" "213" "518"
[73] "420" "500" "235" "325" "299" "518" "299" "299" "420" "299" "518" "358"
[85] "299" "213" "317" "317" "309" "325" "420" "328" "247" "299" "325" "309"
[97] "317" "235" "235" "309" "235" "235" "299" "247" "317" "358" "328" "518"
[109] "299" "518" "247" "325" "420" "299" "299" "325" "325" "309" "420" "213"
min(as.numeric(x4))
[1] 213

Example5

 Live Demo

x5<−sample(c("5112","2542","3241","3211","4125","2568","3742","2784","4269","3627"),120,replace=TRUE)
x5
[1] "2568" "3627" "5112" "4269" "4269" "4125" "4125" "4125" "3627" "3211"
[11] "3241" "3241" "5112" "2784" "3742" "2568" "3211" "5112" "4125" "2784"
[21] "4269" "2784" "3627" "3742" "5112" "3211" "3627" "5112" "3742" "3211"
[31] "3211" "3241" "4269" "5112" "2784" "3241" "3211" "3241" "2542" "4269"
[41] "2542" "2568" "2542" "3241" "4125" "2542" "2568" "4269" "4125" "4269"
[51] "3211" "4269" "2542" "3241" "4125" "4125" "3742" "3627" "3627" "4269"
[61] "3742" "2568" "3241" "2568" "3742" "5112" "4125" "3211" "5112" "3627"
[71] "2784" "2784" "2542" "4269" "3241" "3211" "2568" "2784" "4269" "3241"
[81] "2568" "3627" "3241" "3211" "2568" "3627" "2568" "2568" "3742" "2568"
[91] "4269" "3627" "3627" "2542" "3211" "2542" "5112" "4125" "2542" "3211"
[101] "3241" "2568" "2542" "3211" "3627" "4125" "3627" "5112" "4125" "2784"
[111] "4269" "5112" "3211" "3211" "2542" "5112" "4125" "4269" "3241" "5112"
min(as.numeric(x5))
[1] 2542

Updated on: 06-Nov-2020

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements