Maybe like this? In the file, there is only a single \ before the 0.
DF <- read.csv("c:/users/fjcc/Documents/R/Play/Dummy.csv", stringsAsFactors = FALSE)
DF
#> Name value
#> 1 A \\0
#> 2 b 3
#> 3 C 4
#> 4 D \\0
#> 5 E 6
DF$value <- stringr::str_replace(DF$value, "\\\\0", "0")
DF
#> Name value
#> 1 A 0
#> 2 b 3
#> 3 C 4
#> 4 D 0
#> 5 E 6
Actually I have multiple columns containing "\0" and there are all characters. sample data looks like:
df:
id var1 var2 var3 var4
1 A \0 \0 2
2 B \0 \0 3
3 C \0 \0 \0 5
4 D \0 \0 7
5 E \0 \0 \0 9
I want to use "0" as an integer to replace the columns var1 and var3 which are all "\0" and leave column var2 as it is. I used :
df2 <- apply(df,2,function(x){x=ifelse(x=="\0",0,x)}) %>%
as.data.frame()
But this code changed all "\0" and I noticed that every variable changes from character to factor.
Is there a way to solve this problem? Appreciated your help!