You can use type.convert
and specify "."
as your na.string
:
df <- data ## Create a copy in case you need the original form
df
# A B C D
# 1 1 . 1.2 6
# 2 1 12 . 3
# 3 2 14 1.6 4
df[] <- lapply(df, function(x) type.convert(as.character(x), na.strings="."))
df
# A B C D
# 1 1 NA 1.2 6
# 2 1 12 NA 3
# 3 2 14 1.6 4
Note that the argument is na.strings
(with a plural "s") so you can specify more characters to be treated as NA
values if you have any.
Also, the actual answer to this question might be to simply specify the na.strings
argument when you are first reading your data into R, perhaps with read.table
or read.csv
.
Let's replicate the process of reading a csv from within R:
x <- tempfile()
write.csv(data, x, row.names = FALSE)
read.csv(x)
# A B C D
# 1 1 . 1.2 6
# 2 1 12 . 3
# 3 2 14 1.6 4
read.csv(x, na.strings = ".")
# A B C D
# 1 1 NA 1.2 6
# 2 1 12 NA 3
# 3 2 14 1.6 4