3

Hi I have a strange behaviour of R:

I have a Dataset with Date, Time and some Values:

Date       Time      Global_active_power
2006-12-16 17:24:00  4.216
2006-12-16 17:25:00  5.360
2006-12-16 18:24:00  6.216
2006-12-16 18:25:00  5.390
2006-12-17 07:24:00  3.216
2006-12-17 17:25:00  1.360
2006-12-17 17:24:00  2.216
2006-12-17 18:25:00  6.360

From the first 2 columns I want to construct a combind column that contains Date+Time in 1 column- something like that:

Date       Time      Global_active_power  combinedTime
2006-12-16 17:24:00  4.216                2006-12-16 17:24:00

The problem now is that in doing so I always get the date of Today mixed into it:

Date       Time      Global_active_power  combinedTime
2006-12-16 17:24:00  4.216                2006-12-16 2017-07-23  17:24:00

This is my code:

Data$Date<- as.Date(Data$Date, "%d/%m/%Y")
Data$Time<-strptime(Data$Time, "%H:%M:%S")
Data1 <-subset(Data, (Date < "2007-02-03"  ) )
Data2 <-subset(Data1,(Date > "2007-01-31"  ) )   
t <- paste((Data2$Date), (Data2$Time))
Data2<-cbind(Data2,t)

In my Time column I have only times. I think my problem comes When I run this line in my code :

 Data$Time<-strptime(Data$Time, "%H:%M:%S")

There I suddenly get the date of today plus the time of my column. Why ? I just want the time

Thanks for your help

user3443063
  • 1,455
  • 4
  • 23
  • 37
  • See my [answer](https://stackoverflow.com/questions/45258620/find-rows-in-a-dataframe-with-a-certain-date-using-subset) to your question. – Rui Barradas Jul 23 '17 at 18:04

1 Answers1

1

The problem is indeed where you expected:

> strptime(Data$Time, "%H:%M:%S")
[1] "2017-07-23 17:24:00 CEST" "2017-07-23 17:25:00 CEST" "2017-07-23 18:24:00 CEST" "2017-07-23 18:25:00 CEST"
[5] "2017-07-23 07:24:00 CEST" "2017-07-23 17:25:00 CEST" "2017-07-23 17:24:00 CEST" "2017-07-23 18:25:00 CEST"

strptime also includes todays date, and not only the time. You could solve it like this:

Data$Combined = as.POSIXct(paste0(Data$Date,Data$Time))
Florian
  • 24,425
  • 4
  • 49
  • 80