请原谅我的无知,但我在对数据帧进行排序时遇到了麻烦。我想指定一个有序列表,如c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
,以便按照列表的顺序对数据帧进行排序。
在这个例子中,我想从以下内容开始排序:
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")]),]
但是出现了“undefined columns selected”的错误。如果字符向量中有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")