1

I will need to create a number of reference tables where I specify the number of academic credits a student is supposed to have to achieved, given a certain programme and time period.

I have a list of start dates and programmes for each individual student in the data frame "fulldata". As a first step, I want to use this data frame to get a list of unique start dates for a certain programme. Ideally, I would be able to automate this (and later) step(s) via a function since there are a lot of programmes.

At present I have three programmes, Economics(Ekonomi), Real Estate and Digital Media. I have three corresponding R-objects (containing a vector of academic credits for each module in order) named "Ekonomi", "Real Estate" and "Digital media". I want to fetch the unique values of start_date from "fulldata" where the program name equals the name of my current R object

I write:

start_dates<-function(x){
sd<-fulldata%>%filter(program==deparse(substitute(x)))%>%
dplyr::select(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na()%>%unique()
}

So for start_dates(Ekonomi) the function should fetch the start dates for observations with programme equal to "Ekonomi". This does not seem to work however.

When i write

start_dates(Ekonomi)
sd

It turns out that sd does not contain any observations.

I can write:

sd<-fulldata%>%filter(program==deparse(substitute(Ekonomi)))%>%
dplyr::select(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na()%>%unique()
}

....and then sd turns out completely fine, but I don't seem to be able to do the same thing with a function.

What am I doing wrong and how can I make this work?

Small exerpt of data:

structure(list(UTBILDNINGSTILLFALLE_STARTDATUM = structure(c(15586, 
15586, 15586, 15586, 15586, 15586, 15586, 15586, NA, 15586, 15586, 
NA, 15586, 15586, 15586, NA, 15586, 15586, 15586, 15586), class = "Date"), 
    program = c("Ekonom", "Mäklarekonom", "Ekonom", "Mäklarekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Mäklarekonom", "Ekonom", 
    "Ekonom", "Mäklarekonom", "Ekonom", "Ekonom", "Mäklarekonom", 
    "Mäklarekonom", "Ekonom", "Ekonom", "Mäklarekonom", "Mäklarekonom"
    )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-20L))
zx8754
  • 52,746
  • 12
  • 114
  • 209
Magnus
  • 728
  • 4
  • 17

1 Answers1

1

If you want to pass an unquoted variable to filter, one way is to use rlang::enexpr(x)

library(dplyr)

start_dates<-function(fulldata, x){

  fulldata%>%
    filter(program == as.character(rlang::enexpr(x))) %>%
    distinct(UTBILDNINGSTILLFALLE_STARTDATUM)
}

start_dates(full_data, Ekonom)

# A tibble: 1 x 1
#  UTBILDNINGSTILLFALLE_STARTDATUM
#  <date>                         
#1 2012-09-03             

Passing a quoted variable would not require any of these and can be directly done as

start_dates<-function(fulldata, x){
   fulldata%>%
     filter(program == x) %>%
     distinct(UTBILDNINGSTILLFALLE_STARTDATUM)
}

start_dates(full_data, "Ekonom")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • If I write start_dates<-function(fulldata, x){ fulldata%>% filter(program == x) %>% distinct(UTBILDNINGSTILLFALLE_STARTDATUM) } > sd<-start_dates(fulldata,Ekonom) I get an error message saying that "In program == x : longer object length is not a multiple of shorter object length"? – Magnus Jan 13 '20 at 13:19
  • nvm, I guess that's what you meant by quoted and unquoted variables – Magnus Jan 13 '20 at 13:22
  • Okay, accepting this as correct answer (this somehow reminds me of macros in SAS), thank you for your time! – Magnus Jan 13 '20 at 13:27
  • 1
    @Magnus yes, this is unquoted argument `start_dates(full_data, Ekonom)` and this is quoted `start_dates(full_data, "Ekonom")`. – Ronak Shah Jan 13 '20 at 13:35
  • Unsure if I should post a new question but...the solution with rlang::enexpr works fine on its own, but if I try to use it to create a variable within the function I get an error message saying that "longer object length is not a multiple of shorter object length." – Magnus Jan 13 '20 at 14:14
  • 1
    In NSE, you have to use different functions based on what you want to do. Here we use `as.character(enexpr(x)) ` to convert an unquoted variable to string. If you want to create a new column with the passed value of `x` it requires mostly `!!sym(x) := ` but depending on the context it might vary. Better to ask a new question adding the necessary details. – Ronak Shah Jan 13 '20 at 14:27
  • Right: posting new question: https://stackoverflow.com/questions/59718745/how-do-i-access-use-the-name-of-an-r-object-in-a-function-part-2 – Magnus Jan 13 '20 at 14:39