使用子集查找具有特定日期的数据框中的行 [英] Find rows in a dataframe with a certain date using subset
本文介绍了使用子集查找具有特定日期的数据框中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据帧Date,包含日期,时间和值:
I have a dataframe Date , containing dates , times and values:
Date Time Global_active_power
16/12/2006 17:24:00 4.216
16/12/2006 18:25:00 4.5
17/12/2006 17:25:00 4.52
18/12/2006 17:25:00 4.557
现在我想根据日期查找行-例如所有带日期的行> 16/12/2006。
Now I want to find a row depending on the date - for example all rows with date > 16/12/2006.
这是我的代码:
Data$Date<- as.Date(Data$Date,"%dd%mm%yyyy" )
Data$Time<-strptime(Data$Time, "%h%m%s")
print(class(Data$Date))
print(class(Data$Time))
Data1<-subset(Data, (Date=="16/12/2006" ))
View(Data1)
日期和时间的类别是因素和因素,但现在它们是日期和 POSIXlt POSIXt。当我执行子命令时,Data1为空。
the class of Date and Time were factor and factor but now they are Date and "POSIXlt" "POSIXt" . When I do the subset-command, Data1 is empty.
为什么?它应该包含前2个数据集。
Why? It should contain the first 2 Datasets.
推荐答案
Data$Date<- as.Date(Data$Date,"%d/%m/%Y" ) # date format now 'yyyy-mm-dd'
Data$Time <- as.POSIXct(strptime(Data$Time, "%H:%M:%S"))
print(class(Data$Date))
print(class(Data$Time))
Data1 <- subset(Data, Date > "2006-12-16")
View(Data1)
这篇关于使用子集查找具有特定日期的数据框中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文