[R-sig-Geo] looping over files

Greg Snow 538280 at gmail.com
Sat Mar 3 17:07:07 CET 2012


A couple of options:

Use the paste or sprintf functions to create your file names vector.
This works if you want all combinations, or a very specific set of
names.

Use list.files to get the list of all the file names in the
folder/directory, then use the grep function to extract just the files
that match your pattern.

Either way you can use lapply on the list of files to read and process
the files.

On Sat, Mar 3, 2012 at 4:42 AM, Kanika Jhunjhnuwala
<JhunjhnuwalaK at landcareresearch.co.nz> wrote:
> Hi
>
> I have been trying to loop over lists of files while specifying variables. I am not getting anywhere.
>
> There are basically 3 variables month, year and modelname. My files have the format "tas_",year,"_" ,month,"_", modelname,".txt". I basically want to loop through all the models and put all the data pertaining to that model and year in one list while also naming the list as modelname,"_",year. So basically each list will be 12 long with the files from each month (jan through dec). I have tried using list.files but I do not think you can specify variables in the pattern.
>
> Files<- list.files(path="eg/", pattern="tas_",year,"_" ,month,"_", modelname,".txt", full.names=TRUE)
>
> Does anyone know how to do this? I have 12 models and 2 different years and I want to get all the months of a single year and single model in a single list of files (ie 12 files in a list) with each list specified by the modelname and year.
>
> Thanks
>
> Kanika
>
>
> ________________________________
> Please consider the environment before printing this email
> Warning: This electronic message together with any attachments is confidential. If you receive it in error: (i) you must not read, use, disclose, copy or retain it; (ii) please contact the sender immediately by reply email and then delete the emails.
> The views expressed in this email may not be those of Landcare Research New Zealand Limited. http://www.landcareresearch.co.nz
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-sig-Geo mailing list