R 如何根据时间顺序重新排列数据帧中的行
我有一个数据帧dd,如下所示R 如何根据时间顺序重新排列数据帧中的行,r,R,我有一个数据帧dd,如下所示 sample <- data.frame(Time = c("10:39:00.064 IST", "10:39:00.165 IST", "10:39:00.265 IST", "10:39:00.366 IST", "10:39:00.466 IST", "10:39:00.566 IST", "10:39:00.967 IST", "10:39:01.067 IST", "10:39:01.168 IST",
sample <- data.frame(Time = c("10:39:00.064 IST", "10:39:00.165 IST", "10:39:00.265 IST", "10:39:00.366 IST",
"10:39:00.466 IST", "10:39:00.566 IST", "10:39:00.967 IST", "10:39:01.067 IST", "10:39:01.168 IST",
"10:39:01.269 IST", "10:39:01.369 IST", "10:39:00.665 IST", "10:39:00.765 IST", "10:39:00.866 IST",
"10:39:01.771 IST", "10:39:01.468 IST", "10:39:01.570 IST", "10:39:01.671 IST"),
a = c("A", "D", "A", "C", "D", "W", "T", "R", "Q", "S", "A", "W", "I", "P", "K", "J", "D", "F"),
b = c(8, 3, 9, 9,5,8,9,7,5,6,8,6,5,4,7,5,8,7),
c = c(1, 1, 1, 2, 25,1,4,8,68,59,35,42,45,2,5,7,66,5))
在数据帧示例中,第一列与时间顺序不符
因此,我使用以下代码重新排列行,使其成为特定的顺序
sample[with(sample, order(-Time)), ]
但是,样本没有排序,预期输出如下
Time a b c
10:39:00.064 IST A 8 1
10:39:00.165 IST D 3 1
10:39:00.265 IST A 9 1
10:39:00.366 IST C 9 2
10:39:00.466 IST D 5 25
10:39:00.566 IST W 8 1
10:39:00.665 IST W 6 42
10:39:00.765 IST I 5 45
10:39:00.866 IST P 4 2
10:39:00.967 IST T 9 4
10:39:01.067 IST R 7 8
10:39:01.168 IST Q 5 68
10:39:01.269 IST S 6 59
10:39:01.369 IST A 8 35
10:39:01.468 IST J 5 7
10:39:01.570 IST D 8 66
10:39:01.671 IST F 7 5
10:39:01.771 IST K 7 5
提前感谢使用lubridate,您可以完成
sample <- data.frame(Time = c("10:39:00.064 IST", "10:39:00.165 IST", "10:39:00.265 IST", "10:39:00.366 IST",
"10:39:00.466 IST", "10:39:00.566 IST", "10:39:00.967 IST", "10:39:01.067 IST", "10:39:01.168 IST",
"10:39:01.269 IST", "10:39:01.369 IST", "10:39:00.665 IST", "10:39:00.765 IST", "10:39:00.866 IST",
"10:39:01.771 IST", "10:39:01.468 IST", "10:39:01.570 IST", "10:39:01.671 IST"),
a = c("A", "D", "A", "C", "D", "W", "T", "R", "Q", "S", "A", "W", "I", "P", "K", "J", "D", "F"),
b = c(8, 3, 9, 9,5,8,9,7,5,6,8,6,5,4,7,5,8,7),
c = c(1, 1, 1, 2, 25,1,4,8,68,59,35,42,45,2,5,7,66,5))
library(lubridate)
sample1<- sample
sample1$Time <- hms(sample1$Time)
sample1 <- sample1[order(sample1$Time),]
sample使用sqldf
库:
sqldf("select * from sample order by Time")
输出:
Time a b c
1 10:39:00.064 IST A 8 1
2 10:39:00.165 IST D 3 1
3 10:39:00.265 IST A 9 1
4 10:39:00.366 IST C 9 2
5 10:39:00.466 IST D 5 25
6 10:39:00.566 IST W 8 1
7 10:39:00.665 IST W 6 42
8 10:39:00.765 IST I 5 45
9 10:39:00.866 IST P 4 2
10 10:39:00.967 IST T 9 4
11 10:39:01.067 IST R 7 8
12 10:39:01.168 IST Q 5 68
13 10:39:01.269 IST S 6 59
14 10:39:01.369 IST A 8 35
15 10:39:01.468 IST J 5 7
16 10:39:01.570 IST D 8 66
17 10:39:01.671 IST F 7 5
18 10:39:01.771 IST K 7 5
order(strtime(示例$Time,“%H:%M:%OS”)
Time
是一个因子,不能否定因子;但是,您可以使用xtfrm
将其转换为数字,然后对其进行否定:sample[with(sample,order(-xtfrm(Time)),]
,只要因子级别定义为升序,就可以工作,在这种情况下,它们是通过观察级别(sample$Time)看到的
。有没有可能不更改时间格式来对数据进行排序?!不,在创建数据帧时,您的时间被认为是R中的一个因素,所以最好检查列的类型。@Kumar:怎么了?答案是错的?你能告诉我有什么错误吗?如果数据文件名是以整数形式给出的,语法是“sqldf(“select*from sample1.0 order by Time”),那么我们得到的错误是“error in result_create”(conn@ptr,语句):接近“.0”:语法错误”,它不会对数据进行排序。但是,通过将文件名更改为普通字符,它可以工作。@Kumar:您的数据已经存储在示例数据.frame
中。它适用于您在问题中共享的数据。@Kumar:当然,您在评论中提到的语法也是错误的。
Time a b c
1 10:39:00.064 IST A 8 1
2 10:39:00.165 IST D 3 1
3 10:39:00.265 IST A 9 1
4 10:39:00.366 IST C 9 2
5 10:39:00.466 IST D 5 25
6 10:39:00.566 IST W 8 1
7 10:39:00.665 IST W 6 42
8 10:39:00.765 IST I 5 45
9 10:39:00.866 IST P 4 2
10 10:39:00.967 IST T 9 4
11 10:39:01.067 IST R 7 8
12 10:39:01.168 IST Q 5 68
13 10:39:01.269 IST S 6 59
14 10:39:01.369 IST A 8 35
15 10:39:01.468 IST J 5 7
16 10:39:01.570 IST D 8 66
17 10:39:01.671 IST F 7 5
18 10:39:01.771 IST K 7 5