-5

I have a matrix called Fires. the variables in matrix are called '1 1' up to '9 9'. So:

Fires$11 = 10, 50, 30

Fires$58 = 3, 65, 12, 4'

I want to be able to call these with X and Y that I can define in a loop:

X = 1

Y = 1

Fires$XY = 10, 50, 30

Does anyone have any ideas?

Community
  • 1
  • 1
  • 5
    With syntax like `Fires$11`, Fires is not a matrix. Please provide a reproducible example of your data. See [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for help. – lmo Aug 25 '16 at 11:32
  • 2
    Study `help("[")`. It specifically refers to your problem (among other useful information). – Roland Aug 25 '16 at 11:36

1 Answers1

0

It looks like you're talking about a data frame, not a matrix. You can use names(Fires) to get a vector of column names, and use that to go from your variables to a column reference:

X <- 1
Y <- 1
column.name <- paste0( X, Y )
Fires[ , column.name ]

EDIT: in fact, you can even skip the match step.

rosscova
  • 5,430
  • 1
  • 22
  • 35
  • Thank you, this is exactly what I needed at the time. I found a simpler way of doing what I wanted though. My error was not realising it was a dataframe. but the Fires[, name] will be useful for me in future. – Michael Chaaya Aug 27 '16 at 04:44
  • That's great. Make sure you close off your question then. If my answer was what helped you, please mark it as accepted. If something else was what helped you, add that as your own answer, then mark it as accepted. That way others will be able to find the solution, and the whole community benefits :) – rosscova Aug 27 '16 at 11:03