Skip to content

Instantly share code, notes, and snippets.

@rhilfi
Created November 13, 2020 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhilfi/9debfb48fbe94d26f968141e71ebfb72 to your computer and use it in GitHub Desktop.
Save rhilfi/9debfb48fbe94d26f968141e71ebfb72 to your computer and use it in GitHub Desktop.
remove_NA_in_String #R
# remove NA in string variables
library(dplyr)
Names = c("Peter","NA", "Gabriel", "John", "Lennon","Stuart")
Country=c("Sweden", "Switzerland", "Cumberland", "Wonderland", "Nowhereland", "Land")
Numbers=c(1,2,3,4,NA,6)
id<- 1:6
data<-data.frame(id, Names, Numbers, Country)
head(data)
str(data)
data<-data %>%
mutate(across(where(is.character), str_remove,"NA"))%>% # this line just removes the NA, result will be an empty string
mutate(across(where(is.character), na_if,""))
# or even shorter
data<-data.frame(id, Names, Numbers, Country)
data<-data %>%
mutate(across(where(is.character), na_if,"NA"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment