简体   繁体   中英

Ordering a character matrix by numerical column

I'm working with a matrix read in from a csv that contains both numbers and characters. This is a smaller matrix, but basically what I'm working with:

[,1] [,2] [,3]         [,4]    [,5]    [,6]    [,7]    [,8]    [,9]
V2  "A"  "1"  "Sample X1"  "34712" "39390" "38858" "38574" "38660" 
V3  "A"  "2"  "Sample X2"  "35333" "39940" "40533" "39936" "40669" 
V4  "A"  "3"  "Sample X3"  "33612" "39601" "38658" "39220" "39465" 
V5  "A"  "4"  "Sample X4"  "34309" "39200" "38597" "39820" "40081" 
V6  "A"  "5"  "Sample X5"  "33637" "39404" "40497" "39388" "40033" 
V7  "A"  "6"  "Sample X6"  "35314" "39522" "40345" "38624" "40306" 
V8  "A"  "7"  "Sample X7"  "35548" "39000" "41408" "38310" "39849" 
V9  "A"  "8"  "Sample X8"  "33972" "39930" "39777" "39582" "39570" 
V10 "A"  "9"  "Sample X9"  "34808" "39857" "39252" "39248" "38465" 
V11 "A"  "10" "Sample X10" "34316" "39798" "39776" "39516" "38812" 
V12 "A"  "11" "Sample X11" "34476" "38581" "39672" "38997" "38794" 
V13 "A"  "12" "Sample X12" "36246" "38809" "37872" "38100" "36925" 
V14 "B"  "1"  "Sample X13" "33642" "40201" "40202" "39320" "40426" 
V15 "B"  "2"  "Sample X14" "33381" "40624" "40349" "41350" "40490" 
V16 "B"  "3"  "Sample X15" "34465" "42096" "41194" "40613" "40416" 
V17 "B"  "4"  "Sample X16" "33957" "41905" "42273" "40710" "40681" 
V18 "B"  "5"  "Sample X17" "33877" "42040" "42226" "40788" "41261" 
V19 "B"  "6"  "Sample X18" "33970" "41860" "41149" "41093" "40877" 
V20 "B"  "7"  "Sample X19" "34745" "42040" "40186" "40862" "41044" 
V21 "B"  "8"  "Sample X20" "34140" "41274" "39880" "40356" "40496" 
V22 "B"  "9"  "Sample X21" "33929" "40652" "41410" "40760" "40718" 
V23 "B"  "10" "Sample X22" "33684" "39220" "40478" "41500" "40094"
V24 "B"  "11" "Sample X23" "33141" "41446" "41121" "40726" "41020"
V25 "B"  "12" "Sample X24" "33405" "38481" "37716" "38562" "38218" 
V26 "C"  "1"  "Sample X25" "71560" "86402" "85614" "84273" "83264" 
V27 "C"  "2"  "Sample X26" "72144" "86266" "88082" "87672" "87356" 
V28 "C"  "3"  "Sample X27" "71946" "90201" "89156" "88386" "88006" 
V29 "C"  "4"  "Sample X28" "71758" "89108" "88225" "86006" "88654" 
V30 "C"  "5"  "Sample X29" "71144" "86558" "88614" "87028" "88809" 
V31 "C"  "6"  "Sample X30" "70504" "89230" "88869" "86653" "86356" 
V32 "C"  "7"  "Sample X31" "67874" "88405" "84878" "84914" "85425" 
V33 "C"  "8"  "Sample X32" "70273" "87865" "87529" "87945" "86172" 

I would like sort the matrix by the second column without headers so it goes:

A 1 . . .
B 1
C 1
A 2
B 2
C 2
A 3
. 
.
.
A 12
B 12
C 12 . . .

I've looked around and found that you can use order:

data <- data[order(data[,2],]

but it comes out like this:

A 1 . . .
B 1
c 1
A 10
B 10
C 10
A 11
B 11
C 11
A 12
B 12
C 12
A 2
B 2
C 2
.
.
.
A 9
B 9
C 9 . . .

Is it because this matrix is a character matrix? How do I go about making only the second column numerical so I can sort it based off that?

Thanks

Having your data in a matrix is a bad idea when you want to have a mix of classes (eg numeric and character) across columns. Instead, you should be using a dataframe.

Ideally, read the data into a dataframe with read.csv or read.table . Otherwise, coerce your matrix to a dataframe with as.data.frame .

Given matrix m (in your case data ):

d <- as.data.frame(m, stringsAsFactors=FALSE)
d[, 3] <- as.numeric(d[, 3]) # coerce the relevant column to numeric
d[order(d[, 3]), ]

Note that you could order the matrix as desired with m[order(as.numeric(m[, 3])), ] , but the resulting columns will still all be character .

NB: The explanation for the sorting behaviour that you witnessed is that for a character vector, anything beginning with a 1 (eg 10 ) comes before 2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM