0

I am trying to access the R variables in a loop in the following way

bes2 = data.frame("id"=c(1,2), "generalElectionVoteW1"=c("Labour","Bla"),
                  "generalElectionVoteW2"=c("x","t"))

general_names <- c("generalElectionVoteW1", "generalElectionVoteW2")
labour_w = bes2[bes2$general_names[1] == "Labour",]

Which will simply result in an empty vector.

general_names is simply used to keep generalElectionVoteW1, ...W2 and many more saved for easier access in a loop.

However if I access them manually like labour_w = bes2[bes2$generalElectionVoteW1 == "Labour",] it works as desired. Where is my mistake?

bes2:

   id generalElectionVoteW1      generalElectionVoteW2
1  1                 Labour                          x
2  2                    Bla                          t

general_names:

"generalElectionVoteW1" "generalElectionVoteW2"
ReRed
  • 343
  • 4
  • 15
  • 2
    Your mistake is that R is 1-indexed, not 0-indexed. so it should be `bes[bes$generalElectionVoteW1 == general_names[1] ,]` – Allan Cameron Feb 10 '20 at 16:24
  • Fair point, thank you. However it didn't change anything. Still empty. – ReRed Feb 10 '20 at 16:27
  • Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! Right now I don't know what the object `bes` is and what the vector `general_names` is used for. – dario Feb 10 '20 at 16:31
  • @ToTom does `bes` even have a column called `general_names` ? – Allan Cameron Feb 10 '20 at 16:36
  • @AllanCameron No, I edited my question now. As you can see it is simply used to loop through the names. bes2 however contains generalElectionVoteW1 etc – ReRed Feb 10 '20 at 16:51
  • 1
    In your edited example you use `labour_w = bes2[bes2$general_names[1] == "Labour",]`. but `bes2` does not have a column named `general_names`. – dario Feb 10 '20 at 16:54
  • I know. But the point is that I want to access `generalElectionVoteW1`. ´general_names` simply contains the the names I want to loop through. @dario – ReRed Feb 10 '20 at 17:00
  • @ToTom See this question: https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value – MrFlick Feb 10 '20 at 17:01
  • 2
    Maybe this helps: `labour_w = bes2[bes2[, general_names[i]] == "Labour",]`. But I suggest that you try to identify the source of your struggle (or the multiple sources) and ask questions (including minimal reproducible examples) that are very specific to these problems. – dario Feb 10 '20 at 17:04
  • @dario Perfect, thanks. Exactly what I needed. Now however it also keeps the ones with NA. Any clue how to get rid of them? (not by removing them from the entire dataset though) – ReRed Feb 10 '20 at 17:08
  • 1
    `labour_w = bes2[!is.na(bes2[, general_names[i]]) & bes2[, general_names[i]] == "Labour",]` but please, if you have additional questions open a new question. The comment section is **not** for lengthy discussions. – dario Feb 10 '20 at 18:22

0 Answers0