我想知道如何在对数据进行排序后检索行号. 让我们想象一下我的矢量是这样的: vec = c("GET FRESH", "EASTENDERS", "WORLD CUP", "SPORT", "DYNASTY" ) 然后我排序 sort(vec)[1] "DYNASTY" "EASTENDERS" "GET FRESH"
让我们想象一下我的矢量是这样的:
vec = c("GET FRESH", "EASTENDERS", "WORLD CUP", "SPORT", "DYNASTY" )
然后我排序
sort(vec) [1] "DYNASTY" "EASTENDERS" "GET FRESH" "SPORT" "WORLD CUP"
我怎么能得到每个案例的行号?
vec rownumber [1,] "DYNASTY" "5" [2,] "EASTENDERS" "2" [3,] "GET FRESH" "1" [4,] "SPORT" "4" [5,] "WORLD CUP" "3"尝试使用index.return = TRUE.它返回一个排序值列表和索引,可以转换为’data.frame’
data.frame(sort(vec, index.return=TRUE)) # x ix #1 DYNASTY 5 #2 EASTENDERS 2 #3 GET FRESH 1 #4 SPORT 4 #5 WORLD CUP 3