For some weeks I measured data once a week. Since the measuring instrument was used elsewhere in between I now got quite a few .csv
-Files that I read into R and got them in my workspace now. I named them alike so that all of the data.frames
contain the pattern "11." in their variable name. I know I can get a list of (only) those variable names by using
ls(pattern = "11.")
But I want to have a list
containing those data.frames
. Of course I could go back in my script and change the read.table
command from e.g.
A.11.01 <- read.table(...)
to
data.list[1] <- read.table(...)
and later change the name of the list element data.list[1]
to "A.11.01" (and would have saved a nice amount of time if I had done this immediately) and I'm quite sure I could also find out how to do this defining and naming of a list element in only one command, but it feels as if there is quite a simple option to let R create this list at all.
(Another approach I tried was data.list[1] <- .GlobalEnv[1]
, but I ended up finding no way to subset an environment
. (My other approaches seem too silly to be mentioned at all.))
You could use
mylist <- mget(ls(pattern = "11."))
or, if you want to make sure that you only get data.frame
s with that pattern (not other objects) you could use
mylist <- Filter(is.data.frame, mget(ls(pattern = "11.")))
By the way, in your situation, it would have been easy to read them into a list directly using something like
listfiles <- list.files("path/to/folder", pattern = "\\.csv$", full.names=TRUE)
mylist <- lapply(listfiles, read.csv, stringsAsFactors = FALSE)
(stringsAsFactors = FALSE
is just an example to show you that you can easily add other arguments)