1

TLDR: how to take the nth element from a list to use it as an input for a function.

I get three different excel files in my mail every day: Ayyyymmdd.xls, Byyyymmdd.xls and Cyyyymmdd.xls (the file names change every day; "yyyy" is year, "mm" is month and "dd" is day) .

I need to read these files in R. To do this, I created a list that changes according to today's date. Today, the list looks as follows:

files <- as.list(c("A20180829.xls", "B20180829.xls", "C20180829.xls")

I now need to read all three files separately (using lapply works, but I want to reference only one element at a time). Unfortunately, the following command doesn't work:

Atoday <- read.xls(files[1], sheet = 1)            # Doesn't work
Atoday <- read.xls("A20180829.xls", sheet = 1)     # Works

My main concern is how to take the nth element from a list to use it as an input for a function. The function doesn't need to be read.xls, for all I care, it could be read.csv(), fread(), etc.

smci
  • 26,085
  • 16
  • 96
  • 138
Arturo Sbr
  • 2,773
  • 1
  • 10
  • 34
  • 4
    Read all in to one [list-of-frames](http://stackoverflow.com/a/24376207/3358272): `alldat – r2evans Aug 29 '18 at 22:19
  • 1
    `lapply` or `sapply` will do this, iterating applying a command over the entire list. – smci Aug 29 '18 at 22:22
  • But this is exactly what `lapply` is for: *"take the nth element from a list to use it as an input for a function"*. What do you mean by *"using `lapply` works, but I want to reference only one element at a time"*? `lapply` does only reference only one element at a time. I don't see you have any reason not to use `lapply`. – smci Sep 01 '18 at 04:45
  • Oh, I meant that I specifically did not want to use ´lapply´. I wanted to find a way to refer to just one argument in a list. For example, instead of reading all the files, I wanted to know how to read the 20th file. – Arturo Sbr Sep 03 '18 at 16:45
  • The only reason `read.xls(files[1], ...)` doesn't work is because **[you need `files[[1]` to index into a list](https://cran.r-project.org/doc/manuals/R-intro.html#Lists)**. (Instead of just posting it with "Doesn't work" you should try to figure out why and fix it...) – smci Apr 26 '19 at 00:16
  • I wrote you a canonical question and answer for that [Why does indexing into a list with list instead of list in R not do what you would expect?](https://stackoverflow.com/questions/55859382/why-does-indexing-into-a-list-with-list-n-instead-of-listn-in-r-not-do-wha) – smci Apr 26 '19 at 01:01

1 Answers1

2

It is a list, so we need [[ to extract the list element. The [ is still a list of length 1

read.xls(files[[1]], sheet = 1)

We can find the difference with str or by typing it in the console

files[1]
#[[1]]
#[1] "A20180829.xls"

files[[1]]
#[1] "A20180829.xls"
akrun
  • 674,427
  • 24
  • 381
  • 486
  • This is exactly what I was looking for. Thanks. I don't understand the syntax though. ´files[1]´ returns a list of 1. Why does adding an additional [] turn it into a character string? – Arturo Sbr Aug 29 '18 at 22:28
  • @ArturoSbr As I mentioned, the files is a `list`. Extracting elements of `list` is done with `[[`. If you use a single `[`, it still will not be able to extract that `list`. Please check `?Extract` – akrun Aug 29 '18 at 22:31