0

I have a list of columns names to use in a loop.

As part of this loop I wish to pass i to a statistic,

in this instance i is a character so the following does not work:

quantile(df$i, 0.25)

Although for ggplot2 this function does similar.

aes_string()

What function am I missing here?

Jim
  • 558
  • 4
  • 13
  • 3
    Study `help("[")`. – Roland Jan 14 '20 at 15:34
  • thank you, I shall. – Jim Jan 14 '20 at 15:37
  • The simple answer is that you can't subset a dataframe with `$` (e.g. `df$i`) when the name of the column you want to select is stored in a variable instead of typing the name of the column directly. Instead use `df[[i]]` – natej Jan 14 '20 at 15:58
  • thanks for you answer although this does not seem to be working for me, it does not report an error or an answer – Jim Jan 14 '20 at 16:03
  • turns out (not much help from the annoying closing of the question) that `df[, i]` – Jim Jan 14 '20 at 16:15
  • I don't see why `df[,i]` would work but not `df[[i]]` When asking for help you should include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used for testing. If an existing question has an example and an answer that would work for your question, it's easier to point you to an existing answer. I'm not sure how that's annoying. – MrFlick Jan 14 '20 at 17:37

0 Answers0