Setup environment

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)

####Check directories #### Unlink directories

unlink("Test101",recursive = TRUE)

Reload directory back

folder_path <- "C:\\Users\\LENOVO\\Desktop\\Test101"
destination_folder <- getwd()
file.copy(from = folder_path, to = destination_folder, recursive = TRUE)
## Warning in file.copy(from = folder_path, to = destination_folder, recursive =
## TRUE): problem copying
## C:\Users\LENOVO\Desktop\Test101\000c6417-e1e4-497e-89e6-bb21e17ec355\2017_05_13_10_24_16.csv
## to C:\Users\LENOVO\Documents\R
## Folders\Temp101\Test101\000c6417-e1e4-497e-89e6-bb21e17ec355\2017_05_13_10_24_16.csv:
## Permission denied
## Warning in file.copy(from = folder_path, to = destination_folder, recursive =
## TRUE): problem copying
## C:\Users\LENOVO\Desktop\Test101\000c6417-e1e4-497e-89e6-bb21e17ec355\2017_05_20_18_45_54.csv
## to C:\Users\LENOVO\Documents\R
## Folders\Temp101\Test101\000c6417-e1e4-497e-89e6-bb21e17ec355\2017_05_20_18_45_54.csv:
## Permission denied
## [1] FALSE

Get file names

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

files_in_directory(folder_path)
## File names in the folder:
## 000c6417-e1e4-497e-89e6-bb21e17ec355 
## 0147e40d-85b6-4816-9048-86f03c3d6388

Count number of file in directory

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

Redefine our new folder path in current working directory

Folder in workig directory

root_folder <- "C:\\Users\\LENOVO\\Documents\\R Folders\\Temp101\\Test101"
Function that deletes all json files
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\Test101/000c6417-e1e4-497e-89e6-bb21e17ec355/{000c6417-e1e4-497e-89e6-bb21e17ec355}.json 
## Deleted JSON file: C:\Users\LENOVO\Documents\R Folders\Temp101\Test101/0147e40d-85b6-4816-9048-86f03c3d6388/{0147e40d-85b6-4816-9048-86f03c3d6388}.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] 1597

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:7071507     Min.   :     0   Min.   :  0.000   Min.   :   0.0   
##  Class :character   1st Qu.:  1275   1st Qu.:  5.693   1st Qu.: 145.0   
##  Mode  :character   Median :  2870   Median : 19.100   Median : 204.0   
##                     Mean   :  4950   Mean   : 32.292   Mean   : 192.7   
##                     3rd Qu.:  7011   3rd Qu.: 49.380   3rd Qu.: 256.0   
##                     Max.   :168913   Max.   :245.392   Max.   :1250.0   
##                                                        NA's   :2066870  
##       hr                 cad               alt        
##  Length:7071507     Min.   :  0.0     Min.   :-500.0  
##  Class :character   1st Qu.: 73.0     1st Qu.:  54.8  
##  Mode  :character   Median : 85.0     Median : 130.6  
##                     Mean   : 75.6     Mean   : 384.2  
##                     3rd Qu.: 92.0     3rd Qu.: 482.8  
##                     Max.   :254.0     Max.   :5352.2  
##                     NA's   :2403747   NA's   :197177

Check the number of zero’s in data frame

sum(combined_df$km == 0, na.rm = TRUE)
## [1] 257909
sum(combined_df$power == 0, na.rm = TRUE)
## [1] 623288
sum(combined_df$hr == 0, na.rm = TRUE)
## [1] 21965
sum(combined_df$cad == 0, na.rm = TRUE)
## [1] 427388
sum(combined_df$alt == 0, na.rm = TRUE)
## [1] 6158

Check for mossing 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")
}