blank_to_na | R Documentation |
Replaces blank-ish elements of a factor or character vector to NA
blank_to_na(x, na_strings = c("", ".", "NA", "na", "N/A", "n/a", "NaN", "nan"))
x |
a vector of factor or character or any type |
na_strings |
case sensitive strings that will be coverted to NA. The function will do a trimws(x,'both') before conversion. If NULL, do only trimws, no conversion to NA. |
Returns a vector trimws (always for factor, character) and NA converted (if matching na_strings). Attributes will also be kept ('label','labels', 'value.labels').
https://stackoverflow.com/questions/24172111/change-the-blank-cells-to-na
library(dplyr)
library(tibble)
blank_to_na(x = c("NA", "na", 1, 2, 3))
df <- tibble::tibble(
a = c("NA", "na", 1, 2, 3),
b = factor(c("NA", "na", 1, 2, 3))
)
df
levels(df$b)
df2 <- df %>%
mutate(a = blank_to_na(a),
b = blank_to_na(b))
df2
levels(df2$b)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.