简体   繁体   English

如何按日期对数据框进行排序

[英]How to sort a data frame by date

I need to sort a data frame by date in R.我需要在 R 中按日期对数据框进行排序。 The dates are all in the form of "dd/mm/yyyy".日期都是“dd/mm/yyyy”的形式。 The dates are in the 3rd column.日期在第三列。 The column header is V3. header 列是 V3。 I have seen how to sort a data frame by column and I have seen how to convert the string into a date value.我已经看到了如何按列对数据帧进行排序,并且我已经看到了如何将字符串转换为日期值。 I can't combine the two in order to sort the data frame by date.我不能将两者结合起来按日期对数据框进行排序。

Assuming your data frame is named d ,假设您的数据框名为d

d[order(as.Date(d$V3, format="%d/%m/%Y")),]

Read my blog post, Sorting a data frame by the contents of a column , if that doesn't make sense.如果没有意义,请阅读我的博客文章, 按列的内容对数据框进行排序

Nowadays, it is the most efficient and comfortable to use lubridate and dplyr libraries.如今,使用 lubridate 和 dplyr 库是最有效和最舒适的。

lubridate contains a number of functions that make parsing dates into POSIXct or Date objects easy. lubridate包含许多函数,可以轻松地将日期解析为POSIXctDate对象。 Here we use dmy which automatically parses dates in Day, Month, Year formats.这里我们使用dmy自动解析Day, Month, Year格式的日期。 Once your data is in a date format, you can sort it with dplyr::arrange (or any other ordering function) as desired:一旦您的数据采用日期格式,您可以根据需要使用dplyr::arrange (或任何其他排序函数)对其进行排序:

d$V3 <- lubridate::dmy(d$V3)
dplyr::arrange(d, V3)

In case you want to sort dates with descending order the minus sign doesn't work with Dates.如果您想按降序对日期进行排序,则减号不适用于日期。

out <- DF[rev(order(as.Date(DF$end))),]

However you can have the same effect with a general purpose function: rev().但是,您可以使用通用 function: rev() 获得相同的效果。 Therefore, you mix rev and order like:因此,您将 rev 和 order 混合在一起,如下所示:

#init data
DF <- data.frame(ID=c('ID3', 'ID2','ID1'), end=c('4/1/09 12:00', '6/1/10 14:20', '1/1/11 11:10')
#change order
out <- DF[rev(order(as.Date(DF$end))),]

Hope it helped.希望它有所帮助。

You can use order() to sort date data.您可以使用 order() 对日期数据进行排序。

# Sort date ascending order
d[order(as.Date(d$V3, format = "%d/%m/%Y")),]

# Sort date descending order
d[rev(order(as.Date(d$V3, format = "%d/%m/%y"))),]

Hope this helps,希望这可以帮助,

Link to my quora answer https://qr.ae/TWngCe链接到我的 quora 答案https://qr.ae/TWngCe

Thanks谢谢

If you just want to rearrange dates from oldest to newest in r etc. you can always do:如果您只想在 r 等中将日期从最旧到最新重新排列,您可以随时执行以下操作:

dataframe <- dataframe[nrow(dataframe):1,]

It's saved me exporting in and out from excel just for sort on Yahoo Finance data.它节省了我从 excel 导出和导出的时间,只是为了对 Yahoo Finance 数据进行排序。

The only way I found to work with hours, through an US format in source (mm-dd-yyyy HH-MM-SS PM/AM)...我发现通过源中的美国格式(mm-dd-yyyy HH-MM-SS PM/AM)来处理时间的唯一方法......

df_dataSet$time <- as.POSIXct( df_dataSet$time , format = "%m/%d/%Y %I:%M:%S %p" , tz = "GMT")
class(df_dataSet$time)
df_dataSet <- df_dataSet[do.call(order, df_dataSet), ] 

If you have a dataset named daily_data :如果您有一个名为daily_data的数据集:

daily_data<-daily_data[order(as.Date(daily_data$date, format="%d/%m/%Y")),] 

You could also use arrange from the dplyr library.您还可以使用arrange库中的dplyr

The following snippet will modify your original date string to a date object, and order by it.以下代码段会将您的原始日期字符串修改为日期 object,并按其排序。 This is a good approach, as you store a date as a date, not just a string of characters.这是一个好方法,因为您将日期存储为日期,而不仅仅是字符串。

dates <- dates %>%
  mutate(date = as.Date(date, "%d/%m/%Y")) %>%
  arrange(date)

If you just want to order by the string (usually an inferior option), you can do this:如果您只想按字符串排序(通常是次等选项),您可以这样做:

dates <- dates %>%
  arrange(date = as.Date(date, "%d/%m/%Y"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM