一、数据框的转置
使用t()转置
mtcars2 <- t(mtcars)
二、数据框的排序调整
1、按某一单个变量进行排序
利用rev()实现向量原排序的逆转,但是rev只能对向量使用,所以必须先用rev()求行名的逆序,再利用索引求出按次序排列的数据框。
> ?rev
> letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t"
[21] "u" "v" "w" "x" "y" "z"
> rev(letters)
[1] "z" "y" "x" "w" "v" "u" "t" "s" "r" "q" "p" "o" "n" "m" "l" "k" "j" "i" "h" "g"
[21] "f" "e" "d" "c" "b" "a"
> women
height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
> rownames(women)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
> rev(rownames(women))
[1] "15" "14" "13" "12" "11" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1"
> women[rev(rownames(women)),]
sort()进行升序与降序的排序。系统默认升序排序,实际的控制参数是decreasing=T,decreasing=F。
sort还可以与rev的复合函数实现逆转排序。
> sort(state.name)
[1] "Alabama" "Alaska" "Arizona" "Arkansas"
[5] "California" "Colorado" "Connecticut" "Delaware"
[9] "Florida" "Georgia" "Hawaii" "Idaho"
[13] "Illinois" "Indiana" "Iowa" "Kansas"
[17] "Kentucky" "Louisiana" "Maine" "Maryland"
[21] "Massachusetts" "Michigan"