原諒我的無知,但我在排序數據框時遇到了問題。我想在排序時指定一個有序列表,如c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
,以便數據框按列表順序排序。如何使用R中的列表指定排序順序?
在這個例子中,我想先從
Day Present Count
Fri No 164
Fri Yes 131
Mon No 142
Mon Yes 174
Sat No 39
Sat Yes 26
Sun No 44
Sun Yes 39
Thu No 191
Thu Yes 192
Tue No 184
Tue Yes 214
Wed No 343
Wed Yes 255
,並結束與
Day Present Count
Mon No 142
Mon Yes 174
Tue No 184
Tue Yes 214
Wed No 343
Wed Yes 255
Thu No 191
Thu Yes 192
Fri No 164
Fri Yes 131
Sat No 39
Sat Yes 26
Sun No 44
Sun Yes 39
我已經試過perday[do.call(order, perday[c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")]),]
但我得到「選擇未定義列」。如果字符向量中有14個條目,我也會得到相同的錯誤,所以我現在非常困惑。
這裏的dput:
perday<-structure(list(dayofweek = c("Fri", "Fri", "Mon", "Mon", "Sat",
"Sat", "Sun", "Sun", "Thu", "Thu", "Tue", "Tue", "Wed", "Wed"
), Attended = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("No", "Yes"), class = "factor"),
nrow = c(164L, 131L, 142L, 174L, 39L, 26L, 44L, 39L, 191L,
192L, 184L, 214L, 343L, 255L)), .Names = c("dayofweek", "Attended",
"nrow"), row.names = c(NA, -14L), class = "data.frame")
啊,必須讓他們的因素,然後指定的水平。現在我已經開始在我的導入中設置stringsAsFactors = FALSE,我真的開始學習什麼因素了! –
太好了。這使生活變得輕鬆。 – saudic