I have a dataframe like so:
TS Device1.max Device2.max Device3.max Device4.max
18:02:44 FALSE FALSE TRUE FALSE
18:02:45 TRUE TRUE FALSE FALSE
18:02:46 FALSE FALSE FALSE TRUE
18:02:47 FALSE FALSE FALSE FALSE
18:02:48 FALSE FALSE FALSE FALSE
18:02:49 FALSE FALSE FALSE FALSE
18:02:50 FALSE FALSE FALSE FALSE
18:02:51 FALSE FALSE FALSE FALSE
18:02:52 FALSE FALSE FALSE TRUE
18:02:53 FALSE TRUE FALSE FALSE
18:02:54 FALSE FALSE FALSE FALSE
To get the true false columns I used the following code:
df$Device1.max = ifelse(df$Device1 == max(df$Device1), 'true','false')
df$Device2.max = ifelse(df$Device2 == max(df$Device2), 'true','false')
df$Device3.max = ifelse(df$Device3 == max(df$Device3), 'true','false')
df$Device4.max = ifelse(df$Device4 == max(df$Device4), 'true','false')
For simplicity I am only showing 4 Device columns. I have about a hundred device columns where I would like to do the comparison. It wont be feasible to specify the hundred columns in a hundred ifelse statements
How do I compare using regex or specifying generic column name assuming all the device columns of interest will have some sort of name starting like device?
I then want to filter to or find the row where maximum Device.max columns satisfy the condition where it is TRUE within +/-1 row of it.
Algorithmically, I would create an index column and filter to a dataframe where only the TRUE values are present. Then I would check how many columns have indices within 1 row of each other. In the above case rows 1,2 & 3 have 4 columns satisfying the true condition, whereas rows 9 & 10 have only 2 columns satisfying the condition. Therefore my expected output would be:
TS Device1.max Device2.max Device3.max Device4.max
18:02:44 FALSE FALSE TRUE FALSE
18:02:45 TRUE TRUE FALSE FALSE
18:02:46 FALSE FALSE FALSE TRUE
However this method seems very iterative and inefficient. Is there a better way to do it leveraging dataframe functions in R?