Reputation: 97
I would like to change all the NA to "" in a data.frame made of string. I tried to use the mutate_each function of Dplyr package but it doesn't work (I get numbers in place of the strings).
Df_with_NA <- as.data.frame(rbind(c("toto", "titi", NA, NA), c("tata", "tztz", "tutu", NA), c("toto","titi", "tutu", "tyty")))
empty_as_na <- function(x){
ifelse(is.na(x), "", x)
}
Df_with_empty_string_instead_of_NA <- Df_with_NA %>% mutate_each(funs(empty_as_na))
Can you tell me what is wrong ?
Thank
Upvotes: 1
Views: 1231
Reputation: 32538
#First convert elements of 'Df_with_NA' to character and store in 'df'
#This step is necessary because you didn't use stringsAsFactors = FALSE
#when creating 'Df_with_NA'
df = sapply(Df_with_NA, as.character)
#Then replace NA with ""
df[is.na(df)] = ""
df
# V1 V2 V3 V4
#[1,] "toto" "titi" "" ""
#[2,] "tata" "tztz" "tutu" ""
#[3,] "toto" "titi" "tutu" "tyty"
Upvotes: 2
Reputation: 11893
You should be just fine using plain, old apply()
. Consider:
Df_with_NA <- as.data.frame(rbind(c("toto", "titi", NA, NA),
c("tata", "tztz", "tutu", NA),
c("toto","titi", "tutu", "tyty")))
Df_with_NA
# V1 V2 V3 V4
# 1 toto titi <NA> <NA>
# 2 tata tztz tutu <NA>
# 3 toto titi tutu tyty
empty_as_na <- function(x){
ifelse(is.na(x), "", x)
}
apply(Df_with_NA, 2, empty_as_na)
# V1 V2 V3 V4
# [1,] "toto" "titi" "" ""
# [2,] "tata" "tztz" "tutu" ""
# [3,] "toto" "titi" "tutu" "tyty"
Upvotes: 0