0

For context, this is a continuation for this question

I want to create a function that create a number of values from a vector of academic credits. 1.5 academic credits equal one week of studies in my country, that means I can use a vector of academic credits per module to determine the expected credits on a certain date.

For the sake of clarity, the vector looks like this:

Ekonom<-c(15,15,15,15,7.5,7.5,15,7.5,7.5,15,15,15,30,0)

This, however, presupposes that I couple the vector of academic credits with the correct start date. I can obtain the unique start dates for each programme using the following function:

 start_dates<-function(x){

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

If I input the name of vector of academic credits (named "Ekonomi" for the Economics programme) into this function, I will get the unique start dates from the relevant programme (also named "Ekonomi").

But start date is only one of several values I'm interested in, the full expression is closer to this:

start_dates<-function(x){
summer_break<-ifelse(cumsum(x)<=60, 0, ifelse(cumsum(x)>60 & cumsum(x)<=120 , 1,2))
sd<-fulldata%>%
    filter(program == as.character(rlang::enexpr(x))) %>%
    distinct(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na()
program<-cbind(x,summer_break)
program<-as.data.frame(program)
program<-program%>%rename(points=x)
program<-program%>%mutate(weeks_course=points/1.5)
program<-program%>%mutate(points_expected=lag(cumsum(points)))
program<-program%>%mutate(points_expected=ifelse(is.na(points_expected),0,points_expected))
program<-program%>%mutate(order=1:n())
program<-crossing(sd, program)
program<-program%>%arrange(sd, order)

}

When I use this expression, however, I don't get my start dates. I get an error message claiming that:

In program == as.character(rlang::enexpr(x)) :
  longer object length is not a multiple of shorter object length

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

My desired output looks like this:

structure(list(sd = structure(c(15586, 15586, 15586, 15586, 15586, 
15586, 15586, 15586, 15586, 15586, 15586, 15586, 15586, 15586, 
15950, 15950, 15950, 15950, 15950, 15950, 15950, 15950, 15950, 
15950, 15950, 15950, 15950, 15950, 16314, 16314, 16314, 16314, 
16314, 16314, 16314, 16314, 16314, 16314, 16314, 16314, 16314, 
16314, 16678, 16678, 16678, 16678, 16678, 16678, 16678, 16678, 
16678, 16678, 16678, 16678, 16678, 16678, 17042, 17042, 17042, 
17042, 17042, 17042, 17042, 17042, 17042, 17042, 17042, 17042, 
17042, 17042, 17406, 17406, 17406, 17406, 17406, 17406, 17406, 
17406, 17406, 17406, 17406, 17406, 17406, 17406, 17777, 17777, 
17777, 17777, 17777, 17777, 17777, 17777, 17777, 17777, 17777, 
17777, 17777, 17777, 18141, 18141, 18141, 18141, 18141, 18141, 
18141, 18141, 18141, 18141, 18141, 18141, 18141, 18141), class = "Date"), 
    points_ekon = c(15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0, 15, 15, 15, 15, 7.5, 7.5, 15, 7.5, 7.5, 15, 
    15, 15, 30, 0), summer_break_ekon = c(0, 0, 0, 0, 1, 1, 1, 
    1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 
    2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 
    0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 
    1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 
    2, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 
    1, 1, 1, 1, 1, 1, 2, 2, 2, 2), weeks_course = c(10, 10, 10, 
    10, 5, 5, 10, 5, 5, 10, 10, 10, 20, 0, 10, 10, 10, 10, 5, 
    5, 10, 5, 5, 10, 10, 10, 20, 0, 10, 10, 10, 10, 5, 5, 10, 
    5, 5, 10, 10, 10, 20, 0, 10, 10, 10, 10, 5, 5, 10, 5, 5, 
    10, 10, 10, 20, 0, 10, 10, 10, 10, 5, 5, 10, 5, 5, 10, 10, 
    10, 20, 0, 10, 10, 10, 10, 5, 5, 10, 5, 5, 10, 10, 10, 20, 
    0, 10, 10, 10, 10, 5, 5, 10, 5, 5, 10, 10, 10, 20, 0, 10, 
    10, 10, 10, 5, 5, 10, 5, 5, 10, 10, 10, 20, 0), points_expected = c(0, 
    15, 30, 45, 60, 67.5, 75, 90, 97.5, 105, 120, 135, 150, 180, 
    0, 15, 30, 45, 60, 67.5, 75, 90, 97.5, 105, 120, 135, 150, 
    180, 0, 15, 30, 45, 60, 67.5, 75, 90, 97.5, 105, 120, 135, 
    150, 180, 0, 15, 30, 45, 60, 67.5, 75, 90, 97.5, 105, 120, 
    135, 150, 180, 0, 15, 30, 45, 60, 67.5, 75, 90, 97.5, 105, 
    120, 135, 150, 180, 0, 15, 30, 45, 60, 67.5, 75, 90, 97.5, 
    105, 120, 135, 150, 180, 0, 15, 30, 45, 60, 67.5, 75, 90, 
    97.5, 105, 120, 135, 150, 180, 0, 15, 30, 45, 60, 67.5, 75, 
    90, 97.5, 105, 120, 135, 150, 180), order = c(1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L
    )), row.names = c(NA, -112L), class = c("tbl_df", "tbl", 
"data.frame"))

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))
Magnus
  • 728
  • 4
  • 17
  • That error might have to do with using `==` instead of `filter(program %in% as.character(rlang::enexpr(x)))` – Mike Jan 13 '20 at 14:52
  • Does that presuppose extra parantheses or something? When I try that I get these plus signs to the left, indicating that something is missing. – Magnus Jan 13 '20 at 15:04
  • Does this work for you? `start_dates<-function(x){ fulldata%>% filter(program %in% as.character(rlang::enexpr(x))) %>% distinct(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na() }; start_dates(x = c("Ekonom","Mäklarekonom"))` – Mike Jan 13 '20 at 15:21
  • if you are trying to supply a vector you need to use `%in%` . Also your `rlang` expression might not be necessary here this works as well: `start_dates<-function(x){ fulldata%>% filter(program %in% x) %>% distinct(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na() }; start_dates(x = c("Ekonom","Mäklarekonom"))` – Mike Jan 13 '20 at 15:23
  • 2
    You don't show the code where you are calling the function. What are you passing in? What is the desired output? It's also very odd to keep repeating `program<-program%>%...` on every line. That kind of misses the point of the pipe operator – MrFlick Jan 13 '20 at 16:49
  • I'm assuming you are calling the function by either doing `start_dates("Ekonom")` or `start_dates(Ekonom)`. The first line of your function is `ifelse(cumsum(x)<=60, 0,....` how do you take `cumsum` of character value ? – Ronak Shah Jan 14 '20 at 02:52
  • You guys seem to assume I'm refering to character values, I'm not. I'm referring to a "vector of academic credits", as is stated in the OP. The "Ekonom" vector is numerical and has the values Ekonom<-c(15,15,15,15,7.5,7.5,15,7.5,7.5,15,15,15,30,0) – Magnus Jan 14 '20 at 06:50
  • So I want to refer to the actual values, but at the same time I want to refer to the name of that vector in order to refer to a related column in a different table. – Magnus Jan 14 '20 at 06:52
  • I'll see if I can't solve this by defining two functions, an inner and an outer one. – Magnus Jan 14 '20 at 06:52
  • Adding input and desired output to question. – Magnus Jan 14 '20 at 07:39

0 Answers0