-1

So I use

filenames <- list.files(path="my path", pattern="*.csv")

and then I use assign() and read.csv() to attached an unknown list of .csv in whatever "my path" is.

filename <- c("myFileA","myFileB", "myFileC")

Now myFileA, and myFileB, and myFileC exist as a data frame since I loaded them in.

How to use the filename which contains eg. "myFileA" to pull up data frame myFileA or the other vars?

Basically, I want to manipulate the variables, but because the filenames change, I really have this variable which contains the names:

for (i in filename){

#do something with the dataframe i, BUT
#as is, i is a string "myFileA"
#how to use "myFileA" to pull up dataframe myFileA?

}
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
StudentOfScience
  • 809
  • 3
  • 11
  • 35
  • In the function :) #how to use "myFileA" to pull up dataframe myFileA? – StudentOfScience May 15 '14 at 20:25
  • Your question is rather vague, but if I had to guess I'd say that your mistake was loading all the files into individual, separate data frames. You'd be better off putting them all in a (named) list. That way you can retrieve each one using only its name (as a character). – joran May 15 '14 at 20:30
  • That is an option, but that is not how I have done it. and my way is not wrong. I used filenames <- list.files(path=getwd(), pattern="*.csv") and then I use assign() and read.csv() to attached them. – StudentOfScience May 15 '14 at 20:32
  • 2
    What you describe is generally considered bad practice for exactly the reasons why you're asking a question now. It boxes you into some rather awkward code in order to use your objects. Much better to put them in a list. – joran May 15 '14 at 20:33
  • @joran you first say its bad then you dup it lol mad moding – StudentOfScience May 15 '14 at 20:40
  • 5
    Closing this as a duplicate does not impact you in any way. You got your answer (and some other helpful advice which for some reason you found offensive). Your question is how to retrieve an object using a character of the object name. The direct answer is `get`, which I was quite aware of, I was simply giving you some additional advice while I looked for a duplicate, since this is a very common question. – joran May 15 '14 at 20:42
  • 2
    @joran Indeed. It is in fact [R-FAQ 7.21](http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f), and the advice there too (helpful to those who will take it!) is that it's often easier to use a list ;) – Josh O'Brien May 15 '14 at 21:17
  • 1
    @StudentOfScience you have quite an aggressive tone for someone asking for free advice and receiving not one but two pieces of good advice from Joran. When someone's using `get` it's almost always because they've structured their data poorly. – josliber May 15 '14 at 21:46
  • 3
    everyone here is correct. I apologize. I appreciate everyone's advice. I was asking for something specific given a specific situation, that needed ameliorating. The get() was what I was looking for. – StudentOfScience May 15 '14 at 21:58
  • 2
    No worries. I'm glad you got your answer. – joran May 15 '14 at 22:08
  • 2
    @StudentOfScience, Please have a look at the [**XY problem**](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378) - good to have in mind both when asking and when people suggest alternative solutions to you for free ;) – Henrik May 15 '14 at 22:12

1 Answers1

1

The docs for get say it will, "Search by name for an object"

for (i in filename){

#do something with the dataframe
df <- get(i)

}
cbare
  • 12,060
  • 8
  • 56
  • 63
  • 1
    Instead of unhelpful "do this.. do that.." that is less useful than a flashlight in mid-day, your answer does EXACTLY what I want. Thank you! – StudentOfScience May 15 '14 at 20:38
  • 6
    @StudentOfScience in what sense did the closing as duplicate *not* lead you to the same answer? Instead of griping, perhaps if you'd followed the duplicate link you'd have been just as gushing about joran's pointing you in the right direction. – Gavin Simpson May 15 '14 at 20:46