Skip to content

Commit

Permalink
add file conversion function
Browse files Browse the repository at this point in the history
  • Loading branch information
RayStick committed Jul 25, 2024
1 parent 9d4e002 commit ecb7cfc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
37 changes: 37 additions & 0 deletions R/convert_output.R
Original file line number Diff line number Diff line change
@@ -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)

}


21 changes: 21 additions & 0 deletions man/convert_output.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ecb7cfc

Please sign in to comment.