I have the following dataset. I want to sort it by second column.
dat <- read.table(header=TRUE, text="
ID LFrom LTo It1 It2 It3 It4
ab7 1 2 47 152 259 140
ab8 1.1 2.1 88 236 251 145
ab21 1.2 2.1 72 263 331 147
ab3 1 2 71 207 290 242
ab300 1 2 47 152 259 140
ab4 1.2 2.1 72 263 331 147
ab10 1.1 2 71 207 290 242
ab501 1 2 47 152 259 140
")
dat
ID LFrom LTo It1 It2 It3 It4
1 ab7 1.0 2.0 47 152 259 140
2 ab8 1.1 2.1 88 236 251 145
3 ab21 1.2 2.1 72 263 331 147
4 ab3 1.0 2.0 71 207 290 242
5 ab300 1.0 2.0 47 152 259 140
6 ab4 1.2 2.1 72 263 331 147
7 ab10 1.1 2.0 71 207 290 242
8 ab501 1.0 2.0 47 152 259 140
By using the following code, I find:
dat[with(dat, order(LFrom, ID)),]
ID LFrom LTo It1 It2 It3 It4
4 ab3 1.0 2.0 71 207 290 242
5 ab300 1.0 2.0 47 152 259 140
8 ab501 1.0 2.0 47 152 259 140
1 ab7 1.0 2.0 47 152 259 140
7 ab10 1.1 2.0 71 207 290 242
2 ab8 1.1 2.1 88 236 251 145
3 ab21 1.2 2.1 72 263 331 147
6 ab4 1.2 2.1 72 263 331 147
The sorting in ID is not really sorted according to the number value. I rewrite the data by putting extra 00 and 0 (manually) like the following:
dat1 <- read.table(header=TRUE, text="
ID LFrom LTo It1 It2 It3 It4
ab007 1 2 47 152 259 140
ab008 1.1 2.1 88 236 251 145
ab021 1.2 2.1 72 263 331 147
ab003 1 2 71 207 290 242
ab300 1 2 47 152 259 140
ab004 1.2 2.1 72 263 331 147
ab010 1.1 2 71 207 290 242
ab501 1 2 47 152 259 140
")
dat1
ID LFrom LTo It1 It2 It3 It4
1 ab007 1.0 2.0 47 152 259 140
2 ab008 1.1 2.1 88 236 251 145
3 ab021 1.2 2.1 72 263 331 147
4 ab003 1.0 2.0 71 207 290 242
5 ab300 1.0 2.0 47 152 259 140
6 ab004 1.2 2.1 72 263 331 147
7 ab010 1.1 2.0 71 207 290 242
8 ab501 1.0 2.0 47 152 259 140
Now the following code works fine:
dat1[with(dat1, order(LFrom, ID)), ]
ID LFrom LTo It1 It2 It3 It4
4 ab003 1.0 2.0 71 207 290 242
1 ab007 1.0 2.0 47 152 259 140
5 ab300 1.0 2.0 47 152 259 140
8 ab501 1.0 2.0 47 152 259 140
2 ab008 1.1 2.1 88 236 251 145
7 ab010 1.1 2.0 71 207 290 242
6 ab004 1.2 2.1 72 263 331 147
3 ab021 1.2 2.1 72 263 331 147
I have a large list of dataset. Manually changing the ID is tough. All I need to get the ID sorted (with including 00 and 0).
LFromfirst, thenID. It looks like it's working fine. Not sure what you are asking.