0

I want to create a function to count the values in a certain variable in a subsetted dataset, but my function is not working as it supposed to.

selected_cyl_6 <- subset(mtcars, mtcars$cyl==6)
selected_cyl_4 <- subset(mtcars, mtcars$cyl==4)

count <- function(group,variable) {
  sum(group$variable == 4)
}

count(selected_cyl_6,gear)
# [1] 0 

The answer should be 4. However, if I use the sum directly I get the correct answer

sum(selected_cyl_6$gear==4)
# [1] 4

Another example

count(selected_cyl_4,gear)
# [1] 0
sum(selected_cyl_4$gear==4)
# [1] 8

What am I doing wrong?

KenHBS
  • 6,756
  • 6
  • 37
  • 52
fank
  • 9
  • 1
  • 4

1 Answers1

2

It's from using the dollar sign shortcut in your function. See fortunes::fortune(343).

Some options, using bracket notation.

First, with standard evaluation you would give your variable name in quotes when you use the function.

count <- function(group, variable) {
    sum(group[[variable]] == 4)
}

count(selected_cyl_6, "gear")

If you want to use non-standard evalution so you don't need to quotes, you can use deparse with substitute in your function.

count <- function(group, variable) {
    sum(group[[deparse(substitute(variable))]] == 4)
}

count(selected_cyl_6, gear)
aosmith
  • 34,856
  • 9
  • 84
  • 118