简体   繁体   中英

How to loop over multiple .csv files in R?

I have 7 .csv files (each representing a location - Site1, Site2, Site3... Site7) in one folder and every .csv file contains 5 columns: ID, month, year, Species (with around 30 levels; different species), values(1, 2, 3, 4 - each of the number represents the intensity of flowering). I do not want to combine all the files together, as they contain different species and IDs, but instead I want to apply the same code that does a lot of cuts, interpolation and creates a named list of time series to all of them. At the end, the code gives a list of time series for each Species. At the moment I created a code that can be applied to only one site.

 data_list <- list() #create a list to fill in later 

for(sp in levels(Site1$Species)){
  dat.sp <- Site1[Site1$Species == sp,] # Distinguish between all Species 

  tree_list <- list()

  for (i in unique(dat.sp$ID)){

  other bit of code that cuts the data, 
  interpolates it and makes it as time 
  series. 


  data_list[[sp]]<-tree_list

How should I create the loop to run the same code to every Site ?

You don't need R for this.

Command line solution.

Make sure that no file contains a header
Open command line (cmd)
Enter correct directory (cd C:\yourdirectory)
Write (copy *.csv allcsvsmerged.csv)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM