char2num: Convert Character to Numeric

Description Usage Arguments Value Examples

View source: R/char2num.R

Description

Convert a character vector to a numeric vector, save any non-numeric values to a separate vector of comments.

Usage

1
2
3
4
5
6
char2num(
  x,
  varname = NULL,
  pmissing = c("NA", ".", "", " ", "-"),
  justindex = FALSE
)

Arguments

x

A character vector to be converted to numeric.

varname

A character scalar to be used as an identifier in the generated comment vector. If NULL (default), the name of x will be used. If FALSE, no varname will be used in the comment.

pmissing

A character vector of all possible values to be interpreted as missings (and, therefore, not be commented upon).

justindex

A logical scalar indicating if the output should just be the index of non-numeric values, default FALSE.

Value

By default, a list of length two with the converted numeric vector and the comment vector. If justindex=TRUE, a character vector the same length as x, indicating the elements with non-numeric values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
a <- c(">1.5", "3", "NA", "missing", ".", "12")
char2num(a)
char2num(a, FALSE)
char2num(a, "pH")
char2num(a, pmissing=c("NA", "missing", "."))
char2num(a, justindex=TRUE)

df <- data.frame(
  x1=c(NA, 8.2, 8.2, 8.2, 8.6, 8.1, 8.2),
  x2=c(NA, "83", "*", "83", "79-80", "NA", "83"),
 x3=c(NA, NA, NA, "9.4", "?", ">10", "6.6"))
vars <- c("x2", "x3")
tonum <- lapply(vars, function(v) char2num(df[, v], varname=v))
df2 <- df
df2[, vars] <- data.frame(sapply(tonum, "[", 1))
df2$comment <- apply(do.call(cbind, sapply(tonum, "[", 2)), 1,
 paste, collapse="")

JVAdams/jvamisc documentation built on Aug. 11, 2021, 6:43 a.m.