From 7f5fb24aec470c56df46caac9d46f3cb4d5224b2 Mon Sep 17 00:00:00 2001 From: mrustl Date: Fri, 28 Jun 2024 16:25:52 +0200 Subject: [PATCH] Fix :bug: in read_balance() --- NAMESPACE | 1 + R/read_balance.R | 22 ++++++++++++++++------ R/read_profile.R | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a99451d..1c770cf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,6 +41,7 @@ importFrom(readr,read_delim) importFrom(readr,read_fwf) importFrom(readr,read_table) importFrom(rlang,.data) +importFrom(stats,median) importFrom(stats,setNames) importFrom(stringr,str_detect) importFrom(stringr,str_extract) diff --git a/R/read_balance.R b/R/read_balance.R index 446d84e..c1edc69 100644 --- a/R/read_balance.R +++ b/R/read_balance.R @@ -48,17 +48,27 @@ bal_id_start <- grep("Length", block_sel_txt) bal_id_end <- length(block_sel_txt) -balance <- lapply(block_sel_txt[bal_id_start:bal_id_end], function(x) { - stringr::str_extract_all(x, "\\d+?\\.\\d+")[[1]] %>% as.double() %>% t() %>% - tibble::as_tibble() -}) %>% - dplyr::bind_rows() +# Funktion zur Extraktion der Fließkommazahlen +extract_floats <- function(text) { + as.numeric(unlist(regmatches(text, gregexpr("-?\\d*\\.\\d+(?:E[+-]?\\d+)?", text)))) +} + + +# Anwenden der Funktion auf den gesamten Text +float_numbers_list <- lapply(block_sel_txt[bal_id_start:bal_id_end], extract_floats) + +# Erstellen eines DataFrames mit drei Spalten +balance <- do.call(rbind, lapply(float_numbers_list, function(x) { + length(x) <- 3 # Setze die Länge auf 3, um sicherzustellen, dass alle Einträge drei Spalten haben + return(x) +})) %>% + tibble::as_tibble() names(balance) <- sprintf("id_%d", subregion_ids) parvals <- block_sel_txt[bal_id_start:bal_id_end] %>% - stringr::str_remove_all("-?\\d+?\\.\\d+E\\+?-?\\d\\d") %>% + stringr::str_remove_all("-?\\d*\\.\\d+(?:E[+-]?\\d+)?") %>% stringr::str_trim() parvals_df <- parvals %>% diff --git a/R/read_profile.R b/R/read_profile.R index 76fa2c9..0ac626f 100644 --- a/R/read_profile.R +++ b/R/read_profile.R @@ -5,6 +5,7 @@ #' @return tibble with PROFILE.out data #' @export #' @importFrom stringr str_replace +#' @importFrom stats median read_profile <- function(path) { lines <- readLines(path) @@ -26,7 +27,7 @@ read_profile <- function(path) { stringr::str_split(" ", simplify = TRUE) %>% as.vector() %>% tolower()) - header_clean <- if(median(ncols) > length(header_names_file)) { + header_clean <- if(stats::median(ncols) > length(header_names_file)) { string_conc <- sprintf("conc%d", seq_len(median(ncols) - length(header_names_file))+1) c(stringr::str_replace(header_names_file, "conc", "conc1"),