library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fs)

Folderpath

folder_path <- "C:\\Users\\LENOVO\\Desktop\\Test2"
setwd("C:\\Users\\LENOVO\\Documents\\R Folders\\Temp101")
list.files(path=".", pattern=NULL, all.files=FALSE, 
    full.names=FALSE) 
##  [1] "completeTest2.Rmd"  "echotest.html"      "echotest.Rmd"      
##  [4] "LICENSE"            "local_file"         "local_file.csv"    
##  [7] "mariecurie.jpg"     "packages.bib"       "rsconnect"         
## [10] "Temp101.pdf"        "Temp101.Rmd"        "Temp101.Rproj"     
## [13] "Test101"            "Test2"              "twentysecondcv.cls"
unlink("Test2", recursive = TRUE)
destination_folder <- getwd()
file.copy(from = folder_path, to = destination_folder, recursive = TRUE)
## [1] TRUE

Check file in the folder we’ve copies

root_folder <-"C:\\Users\\LENOVO\\Documents\\R Folders\\Temp101\\Test2"

Get file names

files_in_directory <- function(root_folder) {
  files <- list.files(path = root_folder, full.names = FALSE)
  
  cat("File names in the folder:\n")
  for (file_name in files) {
    cat(file_name, "\n")
  }
}

files_in_directory(root_folder)
## File names in the folder:
## 000c6417-e1e4-497e-89e6-bb21e17ec355 
## 000c6417-e1e4-497e-89e6-bb21e17ec355.zip 
## 000db8a2-a1f6-42bd-8228-fdfae659f476 
## 000e7bee-e589-4d36-962c-cca96323f5d5

Count number of file in directory

cat("Number of folders:",
    length(list.files(path = root_folder, full.names = TRUE))
    , "\n"
    )
## Number of folders: 4

#### Function that deletes all json files

## define function
delete_all_json_files <- function(root_folder) {
  all_json_files <- list.files(path = root_folder,
                               pattern = "\\.json$", 
                               full.names = TRUE, 
                               recursive = TRUE)
  
  for (json_file in all_json_files) {
    file.remove(json_file)
    cat("Deleted JSON file:", json_file, "\n")
  }
}

## Call function
delete_all_json_files(root_folder) 
## Deleted JSON file: C:\Users\LENOVO\Documents\R Folders\Temp101\Test2/000c6417-e1e4-497e-89e6-bb21e17ec355/{000c6417-e1e4-497e-89e6-bb21e17ec355}.json 
## Deleted JSON file: C:\Users\LENOVO\Documents\R Folders\Temp101\Test2/000db8a2-a1f6-42bd-8228-fdfae659f476/{000db8a2-a1f6-42bd-8228-fdfae659f476}.json 
## Deleted JSON file: C:\Users\LENOVO\Documents\R Folders\Temp101\Test2/000e7bee-e589-4d36-962c-cca96323f5d5/{000e7bee-e589-4d36-962c-cca96323f5d5}.json

Creat a list of all dataframes from folder in our root folder

data_list <- root_folder %>% 
  dir_ls(recurse = TRUE, glob = "*.csv") %>% 
  map(
    .f = function(path){
      read_csv(
        path,
        col_types = cols(
          secs = col_number(),
          km = col_double(),
          power = col_double(),
          hr = col_character(),
          cad = col_number(),
          alt = col_double()
        )
      )
    }
  )

Check length of data_list created

length(data_list)
## [1] 457

Lets, create a single dataframeto make arthmetics easy

combined_df <- bind_rows(data_list, .id = "id_column")

Check total missing values in every column

summary(combined_df)
##   id_column              secs              km             power       
##  Length:3873968     Min.   :     0   Min.   :  0.00   Min.   :   0.0  
##  Class :character   1st Qu.:  2473   1st Qu.: 16.68   1st Qu.: 144.0  
##  Mode  :character   Median :  5394   Median : 39.16   Median : 204.0  
##                     Mean   :  6833   Mean   : 46.80   Mean   : 194.1  
##                     3rd Qu.:  9630   3rd Qu.: 69.21   3rd Qu.: 259.0  
##                     Max.   :168913   Max.   :245.39   Max.   :1250.0  
##                                                       NA's   :158876  
##       hr                 cad              alt        
##  Length:3873968     Min.   :  0.0    Min.   : -86.4  
##  Class :character   1st Qu.: 70.0    1st Qu.:  61.2  
##  Mode  :character   Median : 84.0    Median : 313.2  
##                     Mean   : 73.9    Mean   : 586.3  
##                     3rd Qu.: 92.0    3rd Qu.:1149.6  
##                     Max.   :254.0    Max.   :5352.2  
##                     NA's   :791746

Check the number of zero’s in data frame

sum(combined_df$km == 0, na.rm = TRUE)
## [1] 123275
sum(combined_df$power == 0, na.rm = TRUE)
## [1] 483596
sum(combined_df$hr == 0, na.rm = TRUE)
## [1] 9573
sum(combined_df$cad == 0, na.rm = TRUE)
## [1] 343915
sum(combined_df$alt == 0, na.rm = TRUE)
## [1] 1484

Check for missing data

check for files in the data list that have missing data considering each data frame will be a csv file on it’s own

file_paths <- dir_ls(root_folder, recurse = TRUE, glob = "*.csv")
if (length(data_list) == length(file_paths)) {
  # Define a predicate function to check for missing values and delete files
  has_missing_values <- function(df, file_path) {
    if (any(is.na(df))) {
      file_delete(file_path)
      return(TRUE)
    } else {
      return(FALSE)
    }
  }
  
  # Remove data frames with missing values from data_list
  data_list <- map2(data_list, file_paths, has_missing_values)
  
  # Filter out the removed data frames from data_list
  data_list <- data_list[sapply(data_list, Negate(is.null))]
  
} else {
  cat("Length mismatch between data_list and file_paths.\n")
}