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
?s 06:29 de 06/11/2024, Sibylle St?ckli via R-help escreveu:> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, I find it strange that the error is complaining about the 2nd argument, W[[1]]$year. It seems that column W[[1]]$Precip exists but not year. What does str(W[[1]]) return? A data.frame? And why sep = "" when reading the files, aren't those files csv files? 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
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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 11/5/24 22:29, Sibylle St?ckli via R-help 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]]`W[[1]]` should be a data.frame, but it will not have the name "AAR_DailyWeather" unless you assign names using the values in `filelist`.>> dd(data = W[[1]]$Precip, time.scale = W[[1]]$year)It is unclear what this code snippet is supposed to represent. There is no function named `dd` in the base packages. There is a `dd` function in package Hmisc but if you use it you need to assign the results to an object name. Just executing the call will do nothing except perhaps checking to see if there are errors.> Fehler in W[[1]]$year : $ operator is invalid for atomic vectorsAlso unclear is how that error message came about. It seems to indicate that W[[1]] is not a dataframe. You can examine the `W` object with `str(W)`. Looking at the help page for ClimInd::dd it appears that the 'time.scale' argument is supposed to signify what time scale to assume, namely one of {month, season, year}. Pretty crappy documentation since it is unstated what type the allowable values are and the usage example shows |time.scale = YEAR| And there does not appear to be any definition of what the value of `YEAR` is supposed to be. It appears to me that you have been assigned a task in a poorly documented package. Contact the package author. -- David.> > Kind regards > Sibylle > > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.