From ecb7cfcb59cc0717921f09e03e41e873a255063f Mon Sep 17 00:00:00 2001 From: Rachael Stickland <50215726+RayStick@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:31:42 +0100 Subject: [PATCH] add file conversion function --- NAMESPACE | 1 + R/convert_output.R | 37 +++++++++++++++++++++++++++++++++++++ man/convert_output.Rd | 21 +++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 R/convert_output.R create mode 100644 man/convert_output.Rd diff --git a/NAMESPACE b/NAMESPACE index 4399581c..8a0cae22 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(compare_sessions) +export(convert_output) export(domain_mapping) export(user_categorisation) import(cli) diff --git a/R/convert_output.R b/R/convert_output.R new file mode 100644 index 00000000..306e3925 --- /dev/null +++ b/R/convert_output.R @@ -0,0 +1,37 @@ +#' convert_output +#' +#' The 'OUTPUT_' file groups multiple categorisations onto one line e.g. '1,3' \cr \cr +#' This function creates a new longer output 'L-OUTPUT_' which gives each categorisation its own row \cr \cr +#' This 'L-OUTPUT_' may be more useful when using these csv files in later analyses +#' @param output_csv The name of the 'OUTPUT_' csv file that was created from domain_mapping.R +#' @param output_dir The location of output_csv +#' @return The function will return 'L-OUTPUT_' in the same output_dir +#' @export +#' @importFrom utils read.csv write.csv + +convert_output <- function(output_csv,output_dir) { + +output <- read.csv(paste0(output_dir,'/',output_csv)) +output_long <- output[0,] #make duplicate + +for (row in 1:(nrow(output))) { + if (grepl(',',output$Domain_code[row])){ #Domain_code for this row is a list + domain_code_list <- output$Domain_code[row] #extract Domain_code list + domain_code_list_split <- unlist(strsplit(domain_code_list, ",")) #split the list + for (code in 1:(length(domain_code_list_split))){ #for every domain code in list, create a new row + row_to_copy <- output[row,] #extract row + row_to_copy$Domain_code <- domain_code_list_split[code] #change domain code to single + output_long[nrow(output_long) + 1,] = row_to_copy #copy altered row + } + } else { #Domain_code for this row is not list + row_to_copy <- output[row,] #extract row + output_long[nrow(output_long) + 1,] = row_to_copy #copy unaltered row + } +} + +# Save output_long +utils::write.csv(output_long, paste0(output_dir,'/','L-',output_csv),row.names = FALSE) + +} + + diff --git a/man/convert_output.Rd b/man/convert_output.Rd new file mode 100644 index 00000000..5a2cede7 --- /dev/null +++ b/man/convert_output.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/convert_output.R +\name{convert_output} +\alias{convert_output} +\title{convert_output} +\usage{ +convert_output(output_csv, output_dir) +} +\arguments{ +\item{output_csv}{The name of the 'OUTPUT_' csv file that was created from domain_mapping.R} + +\item{output_dir}{The location of output_csv} +} +\value{ +The function will return 'L-OUTPUT_' in the same output_dir +} +\description{ +The 'OUTPUT_' file groups multiple categorisations onto one line e.g. '1,3' \cr \cr +This function creates a new longer output 'L-OUTPUT_' which gives each categorisation its own row \cr \cr +This 'L-OUTPUT_' may be more useful when using these csv files in later analyses +}