Tumaini Kilimba
Tumaini Kilimba

Reputation: 359

R Curly Curly not not picking up variable name for filtering

I am working with Curly Curly {{}} in R for the first time. I tried the below, but that did not work, nothing was filtered and the number of rows in test_data is the same as in result:

library(tidyverse)

test_data = tidyr::tribble(
  ~Date, ~Value,
  "2022-04-01",   5,
  "2022-04-02",   10,
  "2022-03-21",   15
)

censor_data = function(data,censor_date,column){
  
  data %>% 
    filter({{column}} >= censor_date)
}

result = censor_data(
  data = test_data,
  censor_date = "2022-04-01",
  column = "Date"
)

Created on 2022-07-02 by the reprex package (v2.0.1)

However when I remove the curly curlys and explicitly set the column name within the function, it works as expected. What might be the problem?

library(tidyverse)

test_data = tidyr::tribble(
  ~Date, ~Value,
  "2022-04-01",   5,
  "2022-04-02",   10,
  "2022-03-21",   15
)

censor_data = function(data,censor_date){
  
  data %>% 
    filter(Date >= censor_date)
}

result = censor_data(
  data = test_data,
  censor_date = "2022-04-01"
)

Created on 2022-07-02 by the reprex package (v2.0.1)

Upvotes: 1

Views: 94

Answers (1)

langtang
langtang

Reputation: 24845

Call your original function without quoting the column parameter:

result = censor_data(
  data = test_data,
  censor_date = "2022-04-01",
  column = Date
)

Upvotes: 2

Related Questions