问题
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))
来源:https://stackoverflow.com/questions/59718745/how-do-i-access-use-the-name-of-an-r-object-in-a-function-part-2