diff --git a/README.md b/README.md index 80d23d98bb36e245cabf2ea082df6f72fd1bddda..f4c9f79a8c7e94cd79e678a65a611944d1bd6937 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Description of the functions is at the beginning of the function body. To obtain cute_little_R_functions.R file that has to be sourced cute_little_R_functions.docx just for easier code reading -examples_alone.txt compile all the examples of each of the 16 functions into a single file +examples_alone.txt compile all the examples of each of the 17 functions into a single file #### WEB LOCATION @@ -48,6 +48,11 @@ Check for updated versions (more recent release tags) at https://gitlab.pasteur. #### WHAT'S NEW IN +## v4.3.0 + +1) fun_object_info() now manages object class "ordered" "factor" + + ## v4.2.0 1) bug in the fun_2D_comp() function fixed diff --git a/cute_little_R_functions.R b/cute_little_R_functions.R index 731a973d5f5cf97f6a9f7d1e48d2e37c70d9009e..4a8ffced061d812d931279e2632a5555f1f485d5 100644 --- a/cute_little_R_functions.R +++ b/cute_little_R_functions.R @@ -1,6 +1,6 @@ ################################################################ ## ## -## CUTE LITTLE R FUNCTIONS v4.2.0 ## +## CUTE LITTLE R FUNCTIONS v4.3.0 ## ## ## ## Gael A. Millot ## ## ## @@ -15,27 +15,27 @@ ################################ OUTLINE ################################ -################ Object analysis 1 -######## fun_param_check() #### Checking class, type, length, etc. of objects 1 -######## fun_object_info() #### Recovering object information 7 -######## fun_1D_comp() #### comparison of two 1D datasets (vectors, factors, 1D tables) 8 -######## fun_2D_comp() #### comparison of two 2D datasets (row & col names, dimensions, etc.) 11 -######## fun_list_comp() #### comparison of two lists 16 -################ Object modification 18 -######## fun_dataframe_remodeling() #### remodeling a data frame to have column name as a qualitative column and vice-versa 18 -######## fun_refactorization() #### remove classes that are not anymore present in factors or factor columns in data frames 21 -######## fun_rounding() #### Rounding number if decimal present 23 -######## fun_90clock_matrix_rot() #### 90° clockwise matrix rotation 24 -######## fun_hexa_hsv_color_matrix() #### Conversion of a numeric matrix into hexadecimal color matrix 25 -################ Graphics 28 -######## fun_window_width_resizing() #### window width depending on classes to plot 28 -######## fun_open_window() #### Open a GUI or pdf graphic window 29 -######## fun_graph_param_prior_plot() #### Graph param before plotting 32 -######## fun_feature_post_plot() #### Graph param after plotting 35 -######## fun_close_specif_window() #### Closing specific graphic windows 43 -######## fun_quant_var_trim_display() #### Display values from a quantitative variable and trim according to defined cut-offs 45 -################ Exporting results (text & tables) 52 -######## fun_export_data() #### Print string or data object into output file 52 +################ Object analysis 1 +######## fun_param_check() #### Checking class, type, length, etc. of objects 1 +######## fun_object_info() #### Recovering object information 7 +######## fun_1D_comp() #### comparison of two 1D datasets (vectors, factors, 1D tables) 8 +######## fun_2D_comp() #### comparison of two 2D datasets (row & col names, dimensions, etc.) 11 +######## fun_list_comp() #### comparison of two lists 16 +################ Object modification 18 +######## fun_dataframe_remodeling() #### remodeling a data frame to have column name as a qualitative column and vice-versa 18 +######## fun_refactorization() #### remove classes that are not anymore present in factors or factor columns in data frames 21 +######## fun_rounding() #### Rounding number if decimal present 23 +######## fun_90clock_matrix_rot() #### 90° clockwise matrix rotation 24 +######## fun_hexa_hsv_color_matrix() #### Conversion of a numeric matrix into hexadecimal color matrix 25 +################ Graphics 28 +######## fun_window_width_resizing() #### window width depending on classes to plot 28 +######## fun_open_window() #### Open a GUI or pdf graphic window 29 +######## fun_graph_param_prior_plot() #### Graph param before plotting 32 +######## fun_feature_post_plot() #### Graph param after plotting 35 +######## fun_close_specif_window() #### Closing specific graphic windows 43 +######## fun_quant_var_trim_display() #### Display values from a quantitative variable and trim according to defined cut-offs 45 +################ Exporting results (text & tables) 52 +######## fun_export_data() #### Print string or data object into output file 52 ################################ FUNCTIONS ################################ @@ -298,18 +298,23 @@ fun_object_info <- function(data){ # a list containing the info # EXAMPLES # fun_object_info(data = 1:3) + # fun_object_info(list(a = 1:3, b = ordered(factor(c("A", "B"))))) # DEBUGGING # data = NULL # for function debugging # data = 1:3 # for function debugging # data = matrix(1:3) # for function debugging - # data = data.frame(a = 1:3) # for function debugging - # data = factor(1:3) # for function debugging - # data = list(1:3) # for function debugging + # data = data.frame(a = 1:2, b = c("A", "B")) # for function debugging + # data = factor(c("b", "a")) # for function debugging + # data = ordered(factor(c("b", "a"))) # for function debugging + # data = list(a = 1:3, b = factor(c("A", "B"))) # for function debugging + # data = list(a = 1:3, b = ordered(factor(c("A", "B")))) # for function debugging # argument checking # source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.2/r_debugging_tools-v1.2.R") ; eval(parse(text = str_basic_arg_check_dev)) # activate this line and use the function to check arguments status and if they have been checked using fun_param_check() # end argument checking data.name <- deparse(substitute(data)) output <- list("FILE_NAME" = data.name) + tempo <- list("CLASS" = class(data)) + output <- c(output, tempo) tempo <- list("FILE_HEAD" = head(data)) output <- c(output, tempo) if( ! is.null(data)){ @@ -323,19 +328,41 @@ fun_object_info <- function(data){ tempo <- list("SUMMARY" = summary(data)) output <- c(output, tempo) } - if(class(data) == "data.frame" | class(data) == "matrix"){ - tempo <- list("COLUM_NAMES" = names(data)) + if(all(class(data) == "data.frame" | class(data) == "matrix")){ + tempo <- list("ROW_NAMES" = dimnames(data)[[1]]) + output <- c(output, tempo) + tempo <- list("COLUM_NAMES" = dimnames(data)[[2]]) output <- c(output, tempo) } - if(class(data) == "data.frame"){ + if(all(class(data) == "data.frame")){ tempo <- list("STRUCTURE" = ls.str(data)) output <- c(output, tempo) tempo <- list("COLUMN_TYPE" = sapply(data, FUN = "typeof")) + if(any(sapply(data, FUN = "class") %in% "factor")){ # if an ordered factor is present, then sapply(data, FUN = "class") return a list but works with any(sapply(data, FUN = "class") %in% "factor") + tempo.class <- sapply(data, FUN = "class") + if(any(unlist(tempo.class) %in% "ordered")){ + tempo2 <- sapply(tempo.class, paste, collapse = " ") # paste the "ordered" factor" in "ordered factor" + }else{ + tempo2 <- unlist(tempo.class) + } + tempo[["COLUMN_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")] + } output <- c(output, tempo) } - if(class(data) == "list"){ + if(all(class(data) == "list")){ tempo <- list("COMPARTMENT_NAMES" = names(data)) output <- c(output, tempo) + tempo <- list("COMPARTMENT_TYPE" = sapply(data, FUN = "typeof")) + if(any(unlist(sapply(data, FUN = "class")) %in% "factor")){ # if an ordered factor is present, then sapply(data, FUN = "class") return a list but works with any(sapply(data, FUN = "class") %in% "factor") + tempo.class <- sapply(data, FUN = "class") + if(any(unlist(tempo.class) %in% "ordered")){ + tempo2 <- sapply(tempo.class, paste, collapse = " ") # paste the "ordered" factor" in "ordered factor" + }else{ + tempo2 <- unlist(tempo.class) + } + tempo[["COMPARTMENT_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")] + } + output <- c(output, tempo) } return(output) } diff --git a/cute_little_R_functions.docx b/cute_little_R_functions.docx index aa3bdebee1868349e154634230f435dca89c95f4..2062a4de64a9b05144e032bef8d15a824a861250 100644 Binary files a/cute_little_R_functions.docx and b/cute_little_R_functions.docx differ diff --git a/examples_alone.txt b/examples_alone.txt index 1cdc0556ac4700d92840a5775f28fc676d87035c..fcafc77607abfc698dd4a143c7a83cb625060669 100644 --- a/examples_alone.txt +++ b/examples_alone.txt @@ -4,6 +4,7 @@ test <- 1:3 ; fun_param_check(data = test, data.name = NULL, print = TRUE, optio test <- 1:3 ; fun_param_check(data = test, print = TRUE, class = "numeric", typeof = NULL, double.as.integer.allowed = FALSE) fun_object_info(data = 1:3) +fun_object_info(list(a = 1:3, b = ordered(factor(c("A", "B"))))) obs1 = 1:5 ; obs2 = 1:5 ; names(obs1) <- LETTERS[1:5] ; names(obs2) <- LETTERS[1:5] ; fun_1D_comp(obs1, obs2) obs1 = 1:5 ; obs2 = 1:5 ; names(obs1) <- LETTERS[1:5] ; fun_1D_comp(obs1, obs2)