0

Minimum reproducible example:

times <- 0:365
df <- data.frame(times)
for (i in 1:5){
  for(j in 3:6){
    scen_name=paste("Scen",j,i,sep="_")
    scen_out <- sample(times)
    df <- cbind(df, scen_out)
    colnames(df)[length(df)] <- scen_name
  }
}
# Get user input for which variable to view / graph

Selection <- "Scen_4_2"

get(paste0('df$',Selection)))

This fails to retrieve the data.

And exists(paste0('df$',Selection))) returns false - but I can retrieve it perfectly well with df$Scen_4_2.

Note that I need to reference the variable indirectly because the name is being generated - so checking or directly referencing the variable is irrelevant.

David Manheim
  • 2,553
  • 2
  • 27
  • 42
  • I can't do this directly, because I need to be able to loop through columns in a more complex dataframe; df$Var.1, df$Var.2, etc., and I would like to use get() to do so. – David Manheim Aug 03 '23 at 08:29
  • You can loop through the column names, and use `[[` to extract the values. E.g. `for(x in names(df)) { sum(df[[x]]) }` – Darren Tsai Aug 03 '23 at 08:33
  • Great, but that's not what I need to do - there are only specific columns I need. – David Manheim Aug 03 '23 at 08:37
  • See if the solutions in the linked post work in your case. Based on what I understand from your post, I see no reason here to use `get` or `exists`. – zx8754 Aug 03 '23 at 09:09
  • @zx8754 The other solutions, which don't use get or exists, are irrelevant, per the earlier comment which I left to clarify exactly this point, but which was obviously ignored when the question was closed. – David Manheim Aug 03 '23 at 15:11
  • Please provide example input data, example data processing, and expected output. – zx8754 Aug 07 '23 at 07:57
  • Comments were not ignored, I am trying to suggest a better way of doing it. If you can be more specific with your problem, we can give you more direct answer. Please clarify `" I need to reference the variable indirectly because the name is being generated "` with an example code and expected output. – zx8754 Aug 07 '23 at 07:59

1 Answers1

-1

The problem, I now realize, is that a dataframe needs to be specified as an environment to check the columns, or the dataframe needs to be attached.

so:

get(Selection, envir=as.environment(df))

or (though it is generally frowned upon,) if attaching the data frame is acceptable:

attach(df)
get(Selection)
David Manheim
  • 2,553
  • 2
  • 27
  • 42
  • 1
    Attaching is discouraged: https://stackoverflow.com/q/10067680/680068 – zx8754 Aug 03 '23 at 09:12
  • Using `df[['times']]` would be even more direct. That syntax allows for variables as well `a <- "times"; df[["times"]]` or if you just want to know if it exists `"times" %in% names(df)` – MrFlick Aug 03 '23 at 13:53
  • @MrFlick - Sure, but it doesn't do what is needed, since the names are going to vary in the actual application. – David Manheim Aug 07 '23 at 07:53
  • @DavidManheim I don't under stand you comment. `df[[Selection]]` would return the same as `get(Selection, envir=as.environment(df))`. The `[[` operator can take character variables. – MrFlick Aug 07 '23 at 13:38