0

I'm trying to cast an object value as a dataframe column name:

The object input$year contains 2017, and what I currently have is yeardata$2017. I need it to be something like yeardata$(input$year), so that yeardata$ gets input$year as the . This is because the input$year will change as it's the result of a function, but its value will always be present as a column name in the yeardata data frame.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 2
    Could you provide some data to work with? You can find suggestions on how to formulate a good `[r]` question [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Mar 25 '19 at 15:41
  • 2
    You probably need `yeardata[input$year]`. You can only use `$` with literal values. If you need to use variables, then you need proper subsetting with `[]` – MrFlick Mar 25 '19 at 15:43
  • 2
    Be careful with @MrFlick's suggestion ... if `is.numeric(input$year)`, then `yeardat[input$year]` will expect the frame to have (in this case) at least 2,017 columns ... perhaps not the case; it might be safer to try `yeardata[as.character(input$year)]` if in doubt. – r2evans Mar 25 '19 at 16:00
  • MrFlick and r2evans' answers solved my problem. Thanks :) – dgarciagarcia Mar 26 '19 at 09:29

0 Answers0