简体   繁体   中英

Remove factor from dataframe when it provides a certain value

I have following data

 head(df_raw)
  Scan                    Zeit Sensor Response
1    1 04.09.2019 06:28:22:405    101   9936.3
2    2 04.09.2019 06:28:32:389    101   9958.0
3    3 04.09.2019 06:28:42:389    101   9958.0
4    4 04.09.2019 06:28:52:389    101   9979.7
5    5 04.09.2019 06:29:02:389    101   9979.7
6    6 04.09.2019 06:29:12:389    101   9936.3
7    3 04.09.2019 06:28:42:389    102   9958.0
8    4 04.09.2019 06:28:52:389    102   9.9e+37
9    5 04.09.2019 06:29:02:389    102   9.9e+37
10    6 04.09.2019 06:29:12:389    102   9936.3
11    4 04.09.2019 06:28:52:389    103   7563.5
12    5 04.09.2019 06:29:02:389    103   9871.1
13    6 04.09.2019 06:29:12:389    103   10354.8

Sometimes, a sensor is broken and then it provides ~inf. numbers like 9.9e+37 or 9.900e+37 . When a sensor provides such a high value, even only one time, I want to remove that sensor from the dataframe.

In order to remove the entire sensor, then you can do

df[!df$Sensor %in% (df$Sensor[df$Response == 9.90000e+37]),]

which will remove all 102 sensors as shown below,

 Scan Zeit Sensor Response 1 1 04.09.201906:28:22:405 101 9936.3 2 2 04.09.201906:28:32:389 101 9958.0 3 3 04.09.201906:28:42:389 101 9958.0 4 4 04.09.201906:28:52:389 101 9979.7 5 5 04.09.201906:29:02:389 101 9979.7 6 6 04.09.201906:29:12:389 101 9936.3 11 4 04.09.201906:28:52:389 103 7563.5 12 5 04.09.201906:29:02:389 103 9871.1 13 6 04.09.201906:29:12:389 103 10354.8

There are probably smarter ways but an easy way to proceed is:

Convert values to NA

df_raw[df_raw == 9.9e+37]<- NA

Remove columns with NA

not_any_na <- function(x) all(!is.na(x))
df_raw<- df_raw %>%
                    select_if(not_any_na)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM