Home > Enterprise >  R function returns 0, code inside same function when run outside of function returns correct value
R function returns 0, code inside same function when run outside of function returns correct value

Time:10-10

Okay, so I'm new to stackoverflow and R. This is probably a pretty silly problem, but I couldn't find a solution anywhere.

Packages

library(tidyverse)

Here is the the code

sample_size_finder <-  function(variable, value){
  df %>% 
    select(variable) %>%
    filter(variable == value) %>%
    count()
}
  • df = data frame I imported earlier
  • variable = the name of the column I want to select and filter from
  • value = the value in that column I want to filter for

I want the function to look for the column in my data frame specified by the argument "variable", select it, and filter from it all observations that are equal to the argument value. Then, count() all those observations.

When I try to run the function

sample_size_finder("team_size", 4)

it returns

  n
1 0

Which is zero and therefore the wrong output.

Running the code inside without the function wrapped around it with the same arguments

df %>%
    select(team_size) %>%
    filter(team_size == 4) %>%
    count()

returns

   n
1 12

which is the correct output (12 observations for team_size equal to 4).

I narrowed my issue down to the variable argument in my sample_size_finder(variable, value) function. Here is what I tried so far:

  • changed the function to only have "value" as an argument. This gives the desired output, but I want to have the "variable" argument included
  • changed the function to only have "variable" as an argument. This returned "0" as output
  • ran the function without putting "variable" in string quotes. This returned "0" as output

I'm almost ready to give up and just use the code without the function, but somehow this bothers me a lot. It would be so appreciated if a kind soul would help me :)

All the best, Matthias

CodePudding user response:

Use .data to access column names as variable name in the function.

library(dplyr)

sample_size_finder <-  function(variable, value){
  df %>% 
    select(.data[[variable]]) %>%
    filter(.data[[variable]] == value) %>%
    count()
}

sample_size_finder("team_size", 4)

This can be written in base R as -

sample_size_finder <-  function(variable, value){
  sum(df[[variable]] == value)
}

CodePudding user response:

We may use across with all_of

library(dplyr)
sample_size_finder <-  function(variable, value){
  df %>% 
    select(all_of(variable)) %>%
    filter(across(all_of(variable), ~ . == value)) %>%
    count()
}

sample_size_finder("team_size", 4)
  • Related