0

I have data like this.

> head(new3)
        Date Hour Dayahead Actual Difference
1 2015-01-01 0:00    42955  42425        530
2 2015-01-01 0:15    42412  42021        391
3 2015-01-01 0:30    41901  42068       -167
4 2015-01-01 0:45    41355  41874       -519
5 2015-01-01 1:00    40710  41230       -520
6 2015-01-01 1:15    40204  40810       -606

Their characteristics are as below:

> str(new3)
'data.frame':   35044 obs. of  5 variables:
 $ Date      : Date, format: "2015-01-01" "2015-01-01" "2015-01-01" "2015-
  01-01" ...
 $ Hour      : chr  "0:00" "0:15" "0:30" "0:45" ...
 $ Dayahead  : chr  "42955" "42412" "41901" "41355" ...
 $ Actual    : int  42425 42021 42068 41874 41230 40810 40461 40160 39958 
  39671 ...
 $ Difference: chr  "530" "391" "-167" "-519" ...

I tried to change Hour and Dayahead as numberic by doing as.numeric. But it shows me this.

> new3$Dayahead<-as.numeric(new3$Dayahead)
Warning message:
NAs introduced by coercion 
> new3$Hour<-as.numeric(new3$Hour)
Warning message:
NAs introduced by coercion 

So when I checked with str again, it showed me this.

 > str(new3)
'data.frame':   35044 obs. of  5 variables:
 $ Date      : Date, format: "2015-01-01" "2015-01-01" "2015-01-01" "2015-
 01-01" ...
 $ Hour      : num  NA NA NA NA NA NA NA NA NA NA ...
 $ Dayahead  : num  42955 42412 41901 41355 40710 ...
 $ Actual    : int  42425 42021 42068 41874 41230 40810 40461 40160 39958 
 39671 ...
 $ Difference: chr  "530" "391" "-167" "-519" ...

questions is, 1) why do I have 'NAs introduced by coercion' warning message?

2) How can I solve the problem above?

3) Why do I get NA data for Hour and how can I solve it?

Thank you.

junmouse
  • 155
  • 1
  • 9

2 Answers2

2

Try this:

hour <- c("0:00", "0:15", "0:30", "0:45", "1:00", "1:15")

replace the : per . And you could convert

hour <- gsub(":", ".", hour)
hour <- as.numeric(hour)
hour

[1] 0.00 0.15 0.30 0.45 1.00 1.15
Winicius Sabino
  • 1,486
  • 8
  • 17
2

As already mentioned in the comments, if your string contains a non-numeric character (i.e., ":" in your Hour column), you cannot convert it to numeric, that's why you get NA.

I am not sure why do you want to convert your times to numeric, but if you'd like to perform some operations on it (e.g., calculate time differences) then you should convert your dates to Posix format. In your case run:

new3$fulldate <- as.POSIXlt(paste(new3$Date, new3$Hour, sep = " "))
pieca
  • 2,463
  • 1
  • 16
  • 34