1

I have a loop which is running over a vector containing names of a few tens of dataframes that are in my environment. On each iteration, I want to access the dataframe using the name, and access a specific column within it. As I understand it, the best way to access a variable with a string is using get().

But when I try and do this (with name being a variable containing the string "first.name"):

get(column.name, name)

I get the error:

Error in as.environment(pos) : no item called "first.name" on the search list

It does work if I try to run:

get(column.name, first.name)

So, assuming that get() is the right function for this, what am I doing wrong?

Reproduceable example:

my.df <- as.data.frame(x = seq(1:10), y = rnorm(10))

name <- "my.df"
get("x",name)
Henry Brice
  • 280
  • 1
  • 2
  • 18
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You should `get()` the data.frame, then use regular column indexing on the data.frame. Maybe `get(name)[[column.name]]`. But it sounds like things would be easier if you re-wrote to avoid the `get()`. It's rarely the most R-like way to do things. – MrFlick Jun 15 '21 at 16:59
  • 2
    What do you mean by a “list of dataframes”? If you *actually* mean a list, why not use `thelist[[name]][[column_name]]`? `get` is for use with *environments*, and should almost never be used in regular code (it’s generally a sign that the code is more complicated than necessary). – Konrad Rudolph Jun 15 '21 at 17:01
  • @MrFlick Thanks, I've added a reproduceable example. Your solution works, but I would be interested to hear if you have a more R-like way to access a dataframe from a variable with a string that is the name of the dataframe. Thanks in any case! – Henry Brice Jun 16 '21 at 08:42
  • Well, how did you end up in the position where you have the name of a data.frame variable as a string in the first place? That's the real issue. Usually this happens when people try to just port existing code from other languages to R. A more R-like way is to not get to this position in the first place. But if that solution works for you, that's fine. It's just that usually the next step is to try to change the data and things will get even messier when you try to use `assign()`. But if you're not doing that then I guess it doesn't matter. – MrFlick Jun 16 '21 at 16:14
  • @MrFlick It's a set of datasets that is being read from an online source, and I have a csv (read into a data.frame) with the names of the files. So I'm looping over that data.frame with the filenames in order to download the datasets and extract the column I need from each one. – Henry Brice Jun 17 '21 at 17:01
  • 1
    That sounds like it would be much easier if you lapply your data import function to collect the data into a list from the start. – MrFlick Jun 17 '21 at 17:03

1 Answers1

1

We may need to use

get(name)[[column.name]]
akrun
  • 874,273
  • 37
  • 540
  • 662