Home > Software engineering >  How can non standard evaluation in subset lead to unanticipated consequences (=errors) when used as
How can non standard evaluation in subset lead to unanticipated consequences (=errors) when used as

Time:10-20

subset states

For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.

To me it is not clear, how this can lead to errors after reading Advanced R - Non-standard evaluation in subset. Assume I have the following code snippet:

myfun <- function(...) {
  ...
  df <- data.frame(col1 = c("a", "b", NA), col2 = 1:3, col3 = 11:13)
  df_s <- subset(x = df, subset = col1=="a", select = c(col1, col2))
  # In the following I only use df_s in some way
  return(...)
}

To me, this looks save to use in scripts / functions?

Minor issue: Can I include row.names(df_s) <- NULL in subset using ...? I could figure that out...

CodePudding user response:

This warning in the docs comes from the fact that subset() use non-standard evaluation, which makes it hard to use in custom functions when you want to pass custom subsetting conditions to a dataset. This is already addressed in this answer.

In your case however, you want to use subset() in a custom function but only to apply a known subsetting condition to a known dataset, not to pass custom conditions to subset(). There is no problem to evaluate subset() in this case.

  • Related