read through a loop of csv files and return which ones have 30 variables in r

main.r
# List all CSV files in the working directory
file_list <- list.files(pattern = "\\.csv$")

# Create a vector to store names of files with 30 variables
files_with_30_vars <- c()

# Loop through each file and check if it has 30 variables
for(file in file_list) {
  data <- read.csv(file)
  
  if(ncol(data) == 30) {
    files_with_30_vars <- c(files_with_30_vars, file)
  }
}

# Display file names with 30 variables
print(files_with_30_vars)
441 chars
18 lines

related categories

gistlibby LogSnag