makemeNA: Recode Certain Values in a 'data.frame' into 'NA'

Description Usage Arguments Value Author(s) References Examples

Description

A flexible alternative to some of the tricks used to convert certain values into NA after a dataset is already loaded in the workspace. Uses type.convert and na.strings to allow conversion of multiple values into NA.

Usage

1
makemeNA(mydf, NAStrings, fixed = TRUE)

Arguments

mydf

The input data.frame.

NAStrings

The values or a vector of values that should be treated as NA. Alternatively, this can be a regular expression.

fixed

Logical. Defaults to TRUE. Set to FALSE if being used with regular expressions.

Value

A data.frame.

Author(s)

Ananda Mahto

References

http://stackoverflow.com/a/14898521/1270695

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
df1 <- structure(list(
  KY27PHY1 = c("4", "5", "5", "4", "-", "4", "2","3",
               "5", "-", "4", "3", "3", "5", "5"),
  KY27PHY2 = c("4", "4","4", "4", "-", "5", "2", "3",
               "5", "-", "5", "3", "3", "5", "5"),
  KY27PHY3 = c("5", "4", "4", "4", "-", "5", "1", "4",
               "5","-", "4", "3", "3", "5", "5")),
  .Names = c("KY27PHY1", "KY27PHY2","KY27PHY3"),
  row.names = 197:211, class = "data.frame")
df1
makemeNA(df1, "-")

df2 <- data.frame(A = c(1, 2, "-", "not applicable", 5),
                  B = c("not available", 1, 2, 3, 4),
                  C = c("-", letters[1:4]))
df2
makemeNA(df2, "not.*|-", fixed = FALSE)

temp <- structure(
  list(age = c(64.3573, 69.9043, 65.6633, 50.3693,
               57.0334, 81.4939, 56.954, 76.9298),
       CALCIUM = c(1.1, 8.1, 8.6, 8.1, 8.7, 1.1, 9.8, 9.1),
       CREATININE = c(NA, 1.1, 0.8, 1.3, 0.8, NA, 1, 0.8),
       GLUCOSE = structure(c(5L, 4L, 3L, 2L, 6L, 6L, 1L, 6L),
       .Label = c("", "418", "461", "472", "488", "NEG"),
                           class = "factor")),
  .Names = c("age", "CALCIUM", "CREATININE", "GLUCOSE"),
  class = "data.frame", row.names = c(NA, -8L))
temp
## Change anything that is just text to NA
makemeNA(temp, "[A-Za-z]", fixed = FALSE)
## Change any exact matches with "NEG" to NA
makemeNA(temp, "NEG")
## Change any matches with 3-digit integers to NA
makemeNA(temp, "^[0-9]{3}$", fixed = FALSE)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.