In RStudio. Not sure how to replace blanks from certain columns only - and based on their names. Have tried many version of
census_data[c("NAICSP","SOCP") == ""] <- NA
In RStudio. Not sure how to replace blanks from certain columns only - and based on their names. Have tried many version of
census_data[c("NAICSP","SOCP") == ""] <- NA
You may try using apply
in column mode, for a base R option:
cols <- c("NAICSP","SOCP")
census_data[, cols] <- apply(census_data[, cols], 2, function(x) {
ifelse(x == "", NA, x)
})
Disclaimer: This answer uses mde, a package that I happen to have written. If one is open to using packages, one can use recode_as_na
from mde
and provide a subset_cols
vector as follows:
census_data<- data.frame(ID = c("A","B","B","A"),
NAICSP = c("",NA,"Yes","No"),
SOCP = c("","","",""))
# install.packages("devtools")
# devtools::install_github("Nelson-Gon/mde")
mde::recode_as_na(census_data,subset_df=TRUE,
subset_cols = c("NAICSP","SOCP"),
value="")
ID NAICSP SOCP
1 A <NA> NA
2 B <NA> NA
3 B Yes NA
4 A No NA
Warning message:
In recode_as_na.data.frame(census_data, subset_df = TRUE, subset_cols = c("NAICSP", :
Factor columns have been converted to character
NOTE:
The warning message is to remind users of behind the scenes coercion to character.