0

I have a dataframe (df) with variables that look similar to vector-variables:

myvariable[1], myvariable[2] , myvariable[3] , etc.

However, if I want to refer to them, R automatically creates barticks around them:

df$`myvariable[1]`

I want to use those variables within a for-loop, and hence, want to change the number within the brackets automatically. Does anyone know how to do this?

PS: This question is different from other questions insofar as R doesn't see my variables as vector variables but rather as single variables that look the same. Hence, the []-part of my variables is seen as only some kind of string and not as a subsetting operator.

PS2: dput(head(zTT$subjects[, c("myvariable[1]","myvariable[3]","myvariable[4]")],4))

structure(list(\`myvariable[1]\` = c(2, 4, 2, 9), \`myvariable[3]\` = c(1, 
1,2, 3), \`myvariable[4]\` = c(2, 4, 2, 7)), .Names = c("myvariable[1]", 
"myvariable[3]", "myvariable[4]"), row.names = c(NA, 4L), class = "data.frame")
aosmith
  • 34,856
  • 9
  • 84
  • 118
Poza
  • 336
  • 1
  • 16
  • 2
    Use `[[` instead of `$` i.e. `df[[myvariable[1]]]` – akrun Sep 10 '18 at 14:46
  • Thank you so much for your quick reply. Unfortunately, that doesn't work. – Poza Sep 10 '18 at 15:06
  • 1
    Sorry to hear that. It is based on the assumption that you have a data.frame and column names or index are stored in `myvariable` i.e. `df <- data.frame(col1 = 1:5, col2 = 6:10, col3 = 11:15); myvariable <- c('col1', 'col2'); df[[myvariable[1]]]` Also, `doesn't work` can have many meanings – akrun Sep 10 '18 at 15:08
  • When asking for help, you should 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. Use `dput()` to share data so we know exactly what we are dealing with. Also, there are often more efficient ways to write code to loop over columns rather than writing for loops. It would be better to describe more precisely what you are trying to accomplish. – MrFlick Sep 10 '18 at 15:13
  • Sorry for my inconclusive reply. Actually my dataframe consists of several independent variables myvariable[1], myvariable[2] , myvariable[3] , etc. R, therefore, sees the numbers as part of a string, that is why I cannot refer to them that easily. – Poza Sep 10 '18 at 15:20
  • 1
    I'm not sure what other variables you have in your dataset, but you can loop through `names(dataset)` rather than using an index for looping (which may solve the problem). Otherwise you'll probably need to "construct" the names in your loop. For example, if `i` is the indexing number you could do something like `paste0("myvariable[", i, "]")`. ( Maybe add some info on the actual task you are trying to do as this could be an "XY" problem. :) ) – aosmith Sep 10 '18 at 15:35
  • Why are you naming your variables this way? Do they come this way or are you naming them? In R you can loop through each column without every knowning their name using `myDataFrame[row,column]` syntax. So if you wanted columns 2 to 5 you would do `myDataFrame[,2:5]`. If you need to do things by name you can do `myDataFrame[,where(names(myDataFrame) == "myvariable[1]"]` which will return all columns that have a name matching "myvariable[1]". – Adam Sampson Sep 10 '18 at 16:03

1 Answers1

0

As akrun has suggested, you can use [[. The code below uses your own data frame to construct the string which corresponds to the list names.

temp <- structure(list(`myvariable[1]` = c(2, 4, 2, 9),
                       `myvariable[3]` = c(1, 1,2, 3),
                       `myvariable[4]` = c(2, 4, 2, 7)),
                  .Names = c("myvariable[1]", "myvariable[3]",
                             "myvariable[4]"), row.names = c(NA, 4L),
                  class = "data.frame")

for (i in c(1, 3, 4)) {
  myVar <- paste0("myvariable[", i, "]")
  print(temp[[myVar]])

}
hkhalil
  • 16
  • 3