HarHar
HarHar

Reputation: 35

How can I create a loop to make my code more efficient in R?

sorry for the poorly worded question I am new to R.

Basically I am trying to take mean hourly values of acoustic indices recorded several times per hour across many hours and save them together in a data frame.

This is the code for the first 2 hours:

mean_aci <- mean(aci_results$LEFT_CHANNEL)
adi_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy/adi_results.csv")
mean_adi <- mean(adi_results$LEFT_CHANNEL)
aei_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy/aei_results.csv")
mean_aei <- mean(aei_results$LEFT_CHANNEL)
H_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy/H_results.csv")
mean_H <- mean(H_results$LEFT_CHANNEL)
ndsi_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy/ndsi_results.csv")
mean_ndsi <- mean(ndsi_results$LEFT_CHANNEL)

data1 <- data.frame(aci = mean_aci,
                    adi = mean_adi,
                    aei = mean_aei,
                    H = mean_H,
                    ndsi = mean_ndsi)

aci_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (2)/aci_results.csv")
mean_aci <- mean(aci_results$LEFT_CHANNEL)
adi_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (2)/adi_results.csv")
mean_adi <- mean(adi_results$LEFT_CHANNEL)
aei_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (2)/aei_results.csv")
mean_aei <- mean(aei_results$LEFT_CHANNEL)
H_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (2)/H_results.csv")
mean_H <- mean(H_results$LEFT_CHANNEL)
ndsi_results <- read.csv("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (2)/ndsi_results.csv")
mean_ndsi <- mean(ndsi_results$LEFT_CHANNEL)

new_row <- c(mean_aci, mean_adi, mean_aei, mean_H, mean_ndsi)
data1 <- rbind(data1, new_row)

The above code has worked successfully with what I am aiming for, however my problem is that each hour is saved in a different file with numbers corresponding to the hour recorded (i.e. hour - copy, hour - copy (2), hour - copy (3) ... hour - copy (147)).

Manually changing the number for each file for each hour +1 each time seems to be very time consuming and tedious, especially considering I have multiple sites with several hundreds of hours of data in total.

I was wondering if there was any kind of loop which could change the folder of the .csv file to be +1 each time?

Sorry for the long-winded explanation

Upvotes: 0

Views: 57

Answers (1)

akrun
akrun

Reputation: 887471

We can get all the files into list, read it and get the mean

file_path <-  "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy/"


files <- list.files(path = file_path, 
        full.names = TRUE, pattern = "\\.csv$")
lst1 <- lapply(files, function(x) read.csv(x))
out <- sapply(lst1, function(x) mean(x$LEFT_CHANNEL, na.rm = TRUE))

It seems that we need to automate the file_path, i.e paste a number at the end. It can be done in a function

f1 <- function(path, n) {
      file_path <- sprintf('%s (%d)', path, n)
      files <- list.files(path = file_path, 
         full.names = TRUE, pattern = "\\.csv$")
       tmp <- lapply(files, read.csv)
      sapply(tmp, function(x) mean(x$LEFT_CHANNEL, na.rm = TRUE))
 }

If the default case doesn't have anything to add on. We do it outside the function

rbind(out, do.call(rbind, lapply(2:10, f1)))

Upvotes: 3

Related Questions