forked from mlr-org/mlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertColumnNames.R
36 lines (33 loc) · 1.25 KB
/
convertColumnNames.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
#' Removes specials characters in column names.
#'
#' Currently the default bevaviour is:
#' The following special character are eliminated and converted via
#' utf8ToInt to a number.
#' [ ] { } ( ) , + - * / = $ ~
#'
#' @param data [\code{data.frame}]\cr
#' Data to convert.
#' @return [\code{data.frame}]
#'
#' @export
#' @title Removes specials characters in column names.
convertColumnNames = function(data) {
replace = c("\\[", "]", "\\(", ")", "\\{", "}", ",", "\\+", "-", "\\*", "/", "=", "\\$", "~")
# if (missing(replace)) {
# #todo: these are all candidates for bad chars
# #! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
# replace = c("\\[", "]", "\\(", ")", "\\{", "}", ",", "\\+", "-", "\\*", "/", "=", "\\$", "~")
# } else {
# checkArg(replace, "character")
# if(!all(sapply(names(replace), function(x) nchar(x) == 1)))
# stop("All names of 'replace' have to be single characters!")
# }
# replace.collapsed = paste(sapply(replace, function(x) substr(x, nchar(x), nchar(x))), collapse=" ")
cns = colnames(data)
for (bc in replace) {
# take last int code when escaping regexp
cns = gsub(pattern = bc, replacement = rev(utf8ToInt(bc))[1], cns)
}
colnames(data) = cns
return(data)
}