Reputation: 227
I have this df dataset
this is some of columns in my dataset
GarageType GarageFinish GarageQual PavedDrive WoodDeckSF
Attchd RFn TA <NOT AVAILABLE> 0
Attchd Unf TA P 298
Detchd Unf <NOT AVAILABLE> P 300
BuiltIn <NOT AVAILABLE> FA P 80
<NOT AVAILABLE> <NOT AVAILABLE> FA <NOT AVAILABLE> 150
I want only column start with "Garage" and change value NOT AVAILABLE to NA
Be like this
GarageType GarageFinish GarageQual PavedDrive WoodDeckSF
Attchd RFn TA NA 0
Attchd Unf TA P 298
Detchd Unf NA P 300
BuiltIn NA FA P 80
NA NA FA NA 150
I know how to change all of the NOT AVAILABLE to NA in dataframe with this code
df[df=="<NOT AVAILABLE>"]<-NA
But i want it like this, to change NOT AVAILABLE to NA in column with name "Garage"
df %>%
select(starts_with("Garage"))%>%
.....
is.na() %>%
colSums()
i dont know how to fill it
Upvotes: 0
Views: 62
Reputation: 1299
The na_if
function does that for you:
df <- tribble(
~GarageType, ~GarageFinish,
"Attchd", "Rfn",
"Attchd", "Unf",
"Detchd", "Unf",
"BuiltIn", "<NOT AVAILABLE>",
"<NOT AVAILABLE>", "<NOT AVAILABLE>"
)
df %>%
select(starts_with("Garage"))%>%
mutate(across(starts_with("Garage"), ~na_if(., "<NOT AVAILABLE>")))
Upvotes: 1