[R] Using multiple dat files
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Nov 7 11:45:00 CET 2024
Às 18:59 de 06/11/2024, Sibylle Stöckli escreveu:
> Dear Rui
> Dear Bert
> Many thanks
> Solution
> filelist <- list.files(path = "O:/Data-Work/2.../Daten_RA-MeteoCH_1990-2021",
> pattern='*.dat', all.files= T, full.names= T)
> AAR<-read.table(filelist[1])
> It seems therefore that there is no other way than read in individually > 100
> weather tables using read.tables., right? Using file.choose() doesn't change the
> work.
> Yes my .dat files are data.frames
>
>> str(W[[1]]) 'data.frame': 11688 obs. of 7 variables: $ year : num 1990 1990 1990
> 1990 1990 1990 1990 1990 1990 1990 ... $ DOY : num 1 2 3 4 5 6 7 8 9 10 ... $
> Ta : num -2.67 -2.77 -2.23 -2.21 -0.98 0.82 0.49 -1.02 -2.31 -3.36 ... $ Tmin :
> num -3.5 -3.7 -4.26 -2.87 -2.98 0.3 -0.83 -1.27 -3 -3.82 ... $ Tmax : num -1.13
> -0.15 -0.13 -0.45 1 1.87 1.72 -0.35 -0.85 -2.3 ... $ Precip: num 0 0 0 0 0.45
> 1.81 0.03 0 0 0 ... $ rSSD : num 0 0.08 0 0 0.08 0 0 0 0 0 ...
>
> *Gesendet: *Mittwoch, 6. November 2024 um 18:28
> *Von: *"Bert Gunter" <bgunter.4567 using gmail.com>
> *An: *"Sibylle Stöckli" <sibylle.stoeckli using gmx.ch>
> *CC: *r-help using r-project.org
> *Betreff: *Re: [R] Using multiple dat files
> Not quite sure if I understand you.
> list.files() simply returns a character vector(not a list). You can simply use a
> vector index to select whatever file you wish to read. So if your desired
> filename is the 5th element of filelist above, something like
> myfile <- read.table(filename[5], ...)
> You can also use regular expressions to choose a bunch of files that have some
> common signature to their names that you can read in simultaneously using your
> "filelist" vector of names via something like:
> myfiles <- lapply(grep("weath", filelist, value = TRUE), \(x) read.table(x,...))
> ### Note that the result of lapply *is* a list, so use list indexing for
> extraction from myfiles.
> You can also choose files to read interactively (via a GUI interface) using
> file.choose() instead of using list.files() if you prefer to do it that way.
> Cheers,
> Bert
>
> On Wed, Nov 6, 2024 at 8:25 AM Sibylle Stöckli via R-help <r-help using r-project.org
> <mailto:r-help using r-project.org>> wrote:
>
> Dear community
>
> To import multiple .dat weather files I am using list.files().
> I intend to use the R package “ClimInd” to calculate different agroclimatic
> indicators.
>
> Question: Is there another solution to import multiple .dat files so that I
> can select elements from the list, e.g. one specific weather file (example
> AAR_DailyWeather)?
>
>
> # Import multiple .dat files weather data
> filelist <- list.files(path = "O:/Data-Work/……./Daten_RA-MeteoCH_1990-2021",
> pattern='*.dat', all.files= T, full.names= T)
> W <- lapply(filelist, function(x) read.table(x, header = TRUE, sep = "",
> colClasses = "numeric", comment.char = ""))
> W[[1]]
>
> > dd(data = W[[1]]$Precip, time.scale = W[[1]]$year)
> Fehler in W[[1]]$year : $ operator is invalid for atomic vectors
>
> Kind regards
> Sibylle
>
>
>
> ______________________________________________
> R-help using r-project.org <mailto:R-help using r-project.org> mailing list -- To
> UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/
> listinfo/r-help>
> PLEASE do read the posting guide https://www.R-project.org/posting-
> guide.html <https://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
Hello,
If read.table as you have posted it solves the problem of reading one
file, then the following should read all of them.
# No further options passed to read.table. Was that the problem?
AAR_list <- lapply(filelist, read.table)
# You can also set the returned list's names, like Bert "suggested"
AAR_list <- setNames(AAR_list, basename(filelist))
Or you can test in a small subset of the files
# If this works then it's probably safe to read them all
# (and you don't have to, if it doesn't)
AAR_list <- lapply(filelist[1:3], read.table)
Hope this helps,
Rui Barradas
--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com
More information about the R-help
mailing list