如何在写为R中的字符向量的数值向量中找到最小值?

要找到数值矢量的最小值,我们可以直接使用min函数,但是如果使用双反逗号来读取值,则min函数将不起作用。在这种情况下,我们将必须使用带有矢量名称的as.numeric,以便可以在找到最小值之前将其转换为数字形式。例如,如果我们有一个包含1、2、3和4的字符向量,则最小值可以找到为min(as.numeric(x))。

例1

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

例2

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

例子3

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

例子4

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

范例5

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