R/f_cleanColnames.R

Defines functions f_cleanColnames

#' f_cleanColnames
#' 
#' Code adapted from stackoverflow 
#' https://stackoverflow.com/questions/20495598/replace-accented-characters-in-r-with-non-accented-counterpart-utf-8-encoding
#' 
#' @param x character string or vector from which special characters should be removed
#' 
f_cleanColnames <- function(x){

  unwanted_array = list(    'Š'='S', 'š'='s', 'Ž'='Z', 'ž'='z', 'À'='A', 'Á'='A', 'Â'='A', 'Ã'='A', 'Ä'='A', 'Å'='A', 'Æ'='A', 'Ç'='C', 'È'='E', 'É'='E',
                            'Ê'='E', 'Ë'='E', 'Ì'='I', 'Í'='I', 'Î'='I', 'Ï'='I', 'Ñ'='N', 'Ò'='O', 'Ó'='O', 'Ô'='O', 'Õ'='O', 'Ö'='O', 'Ø'='O', 'Ù'='U',
                            'Ú'='U', 'Û'='U', 'Ü'='U', 'Ý'='Y', 'Þ'='B', 'ß'='Ss', 'à'='a', 'á'='a', 'â'='a', 'ã'='a', 'ä'='a', 'å'='a', 'æ'='a', 'ç'='c',
                            'e'='e', 'e'='e', 'ê'='e', 'ë'='e', 'ì'='i', 'í'='i', 'î'='i', 'ï'='i', 'ð'='o', 'ñ'='n', 'ò'='o', 'ó'='o', 'ô'='o', 'õ'='o',
                            'ö'='o', 'ø'='o', 'ù'='u', 'ú'='u', 'û'='u', 'ý'='y', 'ý'='y', 'þ'='b', 'ÿ'='y' )


  x <- gsubfn(paste(names(unwanted_array),collapse='|'), unwanted_array,x)
  #x <- gsub("[^[:alnum:][:blank:]?&/\\-]", "", x)
  x <- gsub(")","", x)
  x <- gsub("[(]","__", x)
  x <- gsub(" ","_", x)
  x <- gsub(" ","", x)
  x <- gsub("^\\_","", x)
  return(x)
  
}
ManuelaRunge/ranalysis documentation built on Dec. 7, 2024, 5:01 a.m.