0 votes
1 view
in R Programming by (5.3k points)

I have a data frame containing (in random places) a character value (say "foo") that I want to replace with an NA.

What's the best way to do so across the whole data frame?

1 Answer

0 votes
by (25.3k points)
edited by

To replace character values in a data frame with NA, you can use the following syntax:

df[ df == "foo" ] <- NA

Note that if you were trying to replace NA with "foo", the reverse will not work

 (df[ df == NA ] = "foo") 

You would need to use 

df[is.na(df)] <- "foo"

If you want to explore more in R programming then watch this R programming tutorial for beginner:

Welcome to Intellipaat Community. Get your technical queries answered by top developers !