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
Now I want to find a row depending on the date - for example all rows with date > 16/12/2006.
This is my code:
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)
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.
Why? It should contain the first 2 Datasets.
There's a problem with the conversion of the
Time
variable. Since it has no date, just a time, when you applystrptime
, it will insert automatically a date, today. This is obviously not what you want. I believe that the best solution is to create a new column,DateTime
.If you want to keep the data/time related variables together, you can swap columns 3 and 4.
Only then you would
Data1 <- subset(...)
.