0

I'm trying to write a list with multiple dataframes (more than 200 dataframes), for that purpose i use the following syntax:

> list.name <- list(ls(pattern="dfname*"))

this let me create a list.name with multiple object and when i try to print the content of the list there is no problem

> list.name
[[1]]
[1] "dfname1" "dfname2" "dfname3" "dfname4" "dfname5"
.....
[200] "dfname200" "dfname201" "dfname202" ....

but, when i try to see a specific dataframe in my list list.name i can't see the dataframe value, for example

> list.name[[5]]
Error en list.name[[5]] : subíndice fuera de  los límites (subscript out of range)

or

> list.name[2]
[[1]]
NULL

I need to built a list that let me do other operations like view str of each dataframe and export each dataframe to csv, etc.

Could you please give me some suggestion? Thanks in advance!

dcaceres
  • 13
  • 2
  • Try using `as.list` instead of `list` in the first code line – gagolews Jun 12 '14 at 16:37
  • You don't understand list indexing. http://stackoverflow.com/questions/1169456/in-r-what-is-the-difference-between-the-and-notations-for-accessing-the – Señor O Jun 12 '14 at 16:41
  • @GavinSimpson - pressed enter to soon looking for link – Señor O Jun 12 '14 at 16:43
  • Thanks gagolew, i already create the list, but still i can't see the dataframe values. For example, i try to see de object allocated in 'list.name[[2]]' and the only thing that i can see is the dataframe name '> list.name[[2]]' [1] "dfname2"'. Thanks – dcaceres Jun 12 '14 at 16:47
  • MrFlick's answer is what you're looking for, take a look at it and try to use it with your data/names, it works like a charm. Also see @Gavin Simpson good explanation. – Jilber Urbina Jun 12 '14 at 16:50
  • +1 I don't see what is wrong here; the OP tried something and is mistaken in how this all works. Being mistaken in how something works doesn't warrant a -1 FFS. What is wrong with people? – Gavin Simpson Jun 12 '14 at 16:50

3 Answers3

1

ls() will just return a character vector with the names of the matching data.frames. If you actually want to create a list of the data.frames you should use

dflist <- mget(ls(pattern="dfname*"))

or if you do just want the names, my not keep it in vector form rather than converting to list

list.name <- list(ls(pattern="dfname*"))

then you can extract each name with

list.name[1]

rather than using the double-bracket syntax. There's really no need for a list in that case.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
0

You have simply created a list of length 1, that contains a vector of 200 names. You haven't stored the dataframes themselves. To actually create a list of all the data frames, you can try

lists <- lapply(ls(pattern="dfname*"), get)
Andrew Barr
  • 3,589
  • 4
  • 18
  • 28
0

You are making a list out of the character vector of object names. You want to get() the object behind each name and make a list from those. In this case, you want the mget() function to get lots of objects at once from their names. This returns a list

## dummy data
dfname1 <- dfname2 <- dfname3 <- data.frame(A = 1:3, B = LETTERS[1:3])

dfnames <- ls(pattern="dfname*")
list.name <- mget(dfnames)
str(list.name)

> str(list.name)
List of 3
 $ dfname1:'data.frame':    3 obs. of  2 variables:
  ..$ A: int [1:3] 1 2 3
  ..$ B: Factor w/ 3 levels "A","B","C": 1 2 3
 $ dfname2:'data.frame':    3 obs. of  2 variables:
  ..$ A: int [1:3] 1 2 3
  ..$ B: Factor w/ 3 levels "A","B","C": 1 2 3
 $ dfname3:'data.frame':    3 obs. of  2 variables:
  ..$ A: int [1:3] 1 2 3
  ..$ B: Factor w/ 3 levels "A","B","C": 1 2 3

list.name[[ dfnames[1] ]]

> list.name[[ dfnames[1] ]]
  A B
1 1 A
2 2 B
3 3 C
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453