makemeNA: Make certain values in a 'data.frame' 'NA'

Description Usage Arguments Author(s) See Also Examples

Description

Sometimes, after having read in data, one needs to replace certain values by NA. One approach is to use mydf[mydf == "some-character"] <- NA. However, in many cases that results in a data.frame where variables which should be numeric end up as characters or factors if the NA string was a character to begin with. This function is a convenience wrapper around type.convert to address such problems.

Usage

1
2
  makemeNA(mydf, NAStrings, fixed = TRUE,
    overwrite = FALSE)

Arguments

mydf

A data.frame in which some values need to be converted to NA

NAStrings

The values which have been used to represent NA

fixed

Logical. Is the NAStrings argument a fixed character (or vector of characters) or a regular expression? Defaults to TRUE.

overwrite

Logical. Should the current object be overwritten? Defaults to FALSE

Author(s)

Ananda Mahto

See Also

type.convert

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Some sample data
temp <- data.frame(
V1 = c(1:3),
V2 = c(1, "*", 3),
V3 = c("a", "*", "c"),
V4 = c(".", "*", "3"))
temp
str(temp)

temp1 <- makemeNA(temp, c("*", "."))
temp1
str(temp1)

# Can make anything NA. Useful for -999 type of NA values
makemeNA(temp, "1")

mrdwab/AMsnippets documentation built on May 23, 2019, 7:15 a.m.