r - replace "." by NA in a data frame where "." is also used as decimal -
i got data frame "." used both decimal marker , alone na.
a b c d 1 . 1.2 6 1 12 . 3 2 14 1.6 4
to work on data frame need obtain:
a b c d 1 na 1.2 6 1 12 na 3 2 14 1.6 4
how should deal keep decimals transform alone "." in column c?
here data in reproducible format:
data <- structure(list(a = c(1l, 1l, 2l), b = c(".", "12", "14"), c = c("1.2", ".", "1.6"), d = c(6l, 3l, 4l)), .names = c("a", "b", "c", "d"), class = "data.frame", row.names = c(na, -3l))
you can use type.convert
, specify "."
na.string
:
df <- data ## create copy in case need original form df # 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 # b c d # 1 1 na 1.2 6 # 2 1 12 na 3 # 3 2 14 1.6 4
note argument na.strings
(with plural "s") can specify more characters treated na
values if have any.
also, actual answer question might specify na.strings
argument when first reading data r, perhaps read.table
or read.csv
.
let's replicate process of reading csv within r:
x <- tempfile() write.csv(data, x, row.names = false) read.csv(x) # b c d # 1 1 . 1.2 6 # 2 1 12 . 3 # 3 2 14 1.6 4 read.csv(x, na.strings = ".") # b c d # 1 1 na 1.2 6 # 2 1 12 na 3 # 3 2 14 1.6 4
Comments
Post a Comment