My R is rusted and I am struggling to find the answer to this rather simple issue. I wish to create a new column based on whether a date entry in the date
column is present in another vector.
To illustrate the issue, I count count the number of rows as follows (this approach works):
sum(as.numeric(block$date == "2019-10-11 06:30:00"))
and it correctly gives me 1
.
Should I do this however:
sum(as.numeric(block$date %in% c("2019-10-11 06:30:00")))
I get 0
, which is a problem since I need check against more than one date-time value.
Sample of data frame as follows:
date Efficiency PowAC PowDC TempCPU TempIGBT failures
1: 2019-10-11 06:30:00 97.77433 488.0686 593.1467 32.04367 49.16300 0
2: 2019-03-18 15:25:00 97.79300 485.2857 590.2600 32.29633 50.02533 0
3: 2019-03-18 15:30:00 97.78000 484.7714 589.6767 32.02700 49.22233 0
4: 2019-03-18 15:35:00 97.78233 482.2714 586.6633 32.26733 49.56700 0
5: 2019-03-18 15:40:00 97.75700 480.3343 585.2167 32.02000 49.18667 0
6: 2019-03-18 15:45:00 97.80400 477.5114 580.5467 32.21833 49.30067 0
7: 2019-03-18 15:50:00 97.79633 474.8886 578.0433 32.02833 48.86067 0
8: 2019-03-18 15:55:00 97.79400 477.2629 581.0667 32.29933 49.45333 0
and dput(block, head(10)
as follows:
library(data.table)
setDT(structure(list(date = structure(c(1546300800, 1546301100, 1546301400,
1546301700, 1546302000, 1546302300, 1546302600, 1546302900, 1546303200,
1546303500), class = c("POSIXct", "POSIXt")), Efficiency = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), PowAC = c(NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN), PowDC = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0), TempCPU = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN), TempIGBT = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN), failures = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
-10L), class = c("data.table", "data.frame"), sorted = "date"))
The vector I am testing against is as follows:
dput(failures)
c("2019-10-11 06:30:00", "2019-10-12 06:30:00", "2019-10-12 17:45:00",
"2019-10-13 06:30:00")