-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfhlp.R
91 lines (77 loc) · 2.92 KB
/
dfhlp.R
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# vi: ft=r fdm=marker
# Is {{{1
################################################################
#' Test type of a data frame.
#'
#' This method tests if the columns of a data frame are all of the same type.
#'
#' @param df The data frame.
#' @param type The type you expect the columns to have. It must be one of the R base types: 'character', 'factor', 'integer', 'numeric' or 'logical'.
#'
#' @return \code{TRUE} or \code{FALSE}.
#'
#' @examples
#' # Test if a data frame contains only integers
#' df <- data.frame(a = c(1, 4), b = c(6, 5))
#' df.is(df, 'integer') # should return FALSE since in R all integers are converted to numeric by default.
#' df.is(df, 'numeric') # should return TRUE.
#'
#' @export
df.is <- function(df, type) {
return(all(vapply(df, function(v) methods::is(v, type), FUN.VALUE = FALSE)))
}
# Force numeric {{{1
################################################################
#' Convert data frame to numeric.
#'
#' Converts integer columns of a data frame into numeric.
#'
#' @param df The data frame.
#' @param cols The set of columns to convert to numeric. By default (when set to \code{NULL}) all integer columns are converted. Set it to a character vector containing the names of the columns you want to convert, or ton integer vector containing the indices of the columns. Can be used to force conversion of non integer columns.
#'
#' @return The converted data frame.
#'
#' @examples
#' # Convert an integer data frame
#' df <- data.frame(a = as.integer(c(1, 4)), b = as.integer(c(6, 5)))
#' df <- df.force.numeric(df)
#'
#' @export
df.force.numeric <- function(df, cols = NULL) {
if ( ! is.null(df)) {
# Convert all columns
if (is.null(cols))
df <- as.data.frame(lapply(df, function(v) if (is.integer(v)) as.numeric(v) else v))
# Convert only the specified columns
else
for (c in cols)
df[[c]] <- as.numeric(df[[c]])
}
return(df)
}
# Read table {{{1
################################################################
#' Data frame loading from a file.
#'
#' Reads a data frame from a file and possibly convert integer columns to numeric. This method calls the built-in \code{read.table()} method and then \code{df.force.numeric()}.
#'
#' @param file The path to the file you want to load. See \code{read.table()\} documentation for more information.
#' @param force.numeric If set to TRUE, all integer columns will be converted to numeric.
#'
#' @return The loaded data frame.
#'
#' @examples
#' \dontrun{}
#' # Load a data frame from a file and convert integer columns
#' df <- df.read.table('/my/path/to/my/csv/file.csv', sep = ',', force.numeric = TRUE)
#' }
#'
#' @export
df.read.table <- function(file, force.numeric = FALSE, ...) {
df <- read.table(file, ...)
if (is.logical(force.numeric) && force.numeric)
df <- df.force.numeric(df)
else if (is.integer(force.numeric) || is.character(force.numeric))
df <- df.force.numeric(df, cols = force.numeric)
return(df)
}