0
getFail = function(df, moduleName, threshold){
  failList = df[df$moduleName<threshold,1:5]
  return(row.names(failList))
}

getFail(class2020,moduleName="Module1",60)

This output returns nothing. I think the problem is that the function isnt picking up moduleName as "Module1"and it is trying to find the column called moduleName.

Is there anyway to access dataframe through a variable.

E.g. moduleName could be "Module2" "Module3" etc.

Phil
  • 7,287
  • 3
  • 36
  • 66
YYY
  • 89
  • 7

1 Answers1

0
getFail = function(df, moduleName, threshold){
  failList = df[df[[moduleName]]<threshold,1:5]
  return(row.names(failList))
}

getFail(class2020,moduleName="Module1",60)

Should work, I couldn't test it but the idea is to use [[variable]] instead of &variable

Ruben
  • 194
  • 2
  • 11
  • Thanks it works. May I ask whats the different though? – YYY Feb 17 '21 at 15:01
  • In the source code (R/src/main/subset.c) it states: **The $ subset operator. We need to be sure to only evaluate the first argument. The second will be a symbol that needs to be matched, not evaluated.** `$` works like a function, you can't pass something which first has to be evaluated, only strings which are never evaluated. Whereas with `[[]]` you can pass arguments which have to be evaluated. Both reduce the dataframe to a vector. – Ruben Feb 17 '21 at 15:22
  • @JasonY `$` can only be used with literal column names. For string column names you must use `[[`. – Gregor Thomas Feb 17 '21 at 15:22
  • Hi @JasonY if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Ruben Feb 18 '21 at 09:26