I have data in which some of the columns have a mix of character
and numeric
data. For example:
Sensor Sensor2 Sensor3
Fail 5 Fail
Fail 5 Fail
Fail 5 Fail
Fail 5 Fail
5 5 Fail
6 5 Fail
8 5 Fail
All sensor values should be numeric
, but R automatically take Sensor1 and Sensor3 as character
values. I want to replace "Fail" with NA
or some numeric values, or change the whole Column type to Numeric type so 'Fail' will be changed to NA
automatically.
My problem is that my data set is really large (sensor data generated every second, with more than 200 variables). I want to identify all the columns that contains 'Fail' and change these columns to numeric
type.
This is what I tried: I wrote a function
function(mydata, value1, value2){
mydata <- data.frame(lapply(mydata, function(x){
gsub(value1, value2 ,x)
}))
}
This function works, but the only problem is that the whole data frame was changed to factor
type. I have a mix of numeric
and character
that I want to keep the data type correct. So I tried
function(mydata, value1, value2){
mydata <- data.frame(lapply(mydata, function(x){
is.numeric(gsub(value1, value2 ,x))
}))
}
This changed my columns all to numeric
, so I missed all the character
column information.
How can I identify only the 'Fail' columns and make the change only to those columns and update my data frame with the change? Thanks.