functions functioning learning

Hello, how are you doing, I am taking an R course and I have reached a point where it asks me to put together some functions which I am not being able to make work, I estimate that it is due to my brief introduction to the subject and I still do not have enough understanding to carry out that activity.

I would like to know if someone can tell me or help me how to perform a function that fulfills 3 activities, one is to choose the destination folder where I want the files to be searched, the second is to allow me to pre-select between one or more files that only have consecutive numbers as names. Starting from 1 to 300 and third point, within those database files, choose which columns I want to see.
it have to look samoethign like this

pollutantmean <- function(directory, pollutant, id = 1:332){

}
Could someone give me some kind of guide?

Can you write the code to search in a directory named May2023 for a file named 42 and read the column named NO2? Please show how you would do that or, if you can't do all of it, show the parts you can do.

something like
if the file is csv
read.csv(.../May2023/42.csv, header = TRUE, sep = " , ", ...)
...thats all i can think right now

Your call to csv contains the directory name May2023 and the file name 42.csv. I'll note that you did not put quotes around ".../May2023/42.csv" which would have caused an error and I thing the directory name should not start with ".../". If you were building the file path up from its parts, you could use the paste0() function.

paste0("May2023/", "42", ".csv")
[1] "May2023/42.csv"

You could put that work into a function, like this

MakePath <- function(directory, file) {
   FullName = paste0(directory, file, ".csv")
   return(FullName)
 }
 MakePath(directory = "May2023/", file = "42")
[1] "May2023/42.csv"

You don't want to just make the file name, you want to use it in read.csv(). Can you figure out how to do that part within the MakePath function?

Also, if you pass in a range of numbers like 40:42, the function will make a vector of file names, one for each number.

MakePath <- function(directory, file) {
   FullName = paste0(directory, file, ".csv")
   return(FullName)
 }
MakePath(directory = "May2023/", file = 40:42)
[1] "May2023/40.csv" "May2023/41.csv" "May2023/42.csv"

Do you know how to loop through that vector and pass each individual value to read.csv()?

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.