Created
November 13, 2020 15:15
-
-
Save rhilfi/9debfb48fbe94d26f968141e71ebfb72 to your computer and use it in GitHub Desktop.
remove_NA_in_String #R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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