mardi 17 décembre 2019

Can the ifelse-function access values calculated within itself?

My data looks something like the following:


Start_term             Term_in_order          Current_term 
HT2012                 1                       HT2012 
HT2012                 2                       VT2013
HT2012                 3                       HT2013
HT2012                 4                       VT2014
HT2012                 5                       HT2014   
HT2012                 6                       VT2015 
HT2012                 -                       HT2015
HT2012                 -                       VT2016
HT2012                 -                       HT2016
HT2012                 .                       VT2017   


[...]

HT2012                 1                       HT2013
HT2012                 2                       VT2014
HT2012                 3                       VT2014

So all values for "period_in_order" exceeding 6 has been omitted. I want to include this data hand have it look more like this:


Start_term             Term_in_order          Current_term 
HT2012                 1                       HT2012 
HT2012                 2                       VT2013
HT2012                 3                       HT2013
HT2012                 4                       VT2014
HT2012                 5                       HT2014   
HT2012                 6                       VT2015 
HT2012                 7                       HT2015
HT2012                 8                       VT2016
HT2012                 9                       HT2016
HT2012                 10                      VT2017   


[...]

HT2012                 1                       HT2013
HT2012                 2                       VT2014
HT2012                 3                       VT2014

[...]

I try to achieve this through the following code:

HT2018resultat<-HT2018resultat%>%mutate(PERIOD_I_ORDNING=
ifelse(as.character(STARTPERIOD_KOD)==as.character(PERIOD_KOD),1,lag(PERIOD_I_ORDNING)+1))

So whenever the start_term equals the current_term the value of period_in_order is set to 1 and incremented by 1 for each row until the start_term equals current_term again. Then the value of period_in_order is set to 1 again and the process repeats itself.

However, the code doesn't work and the output looks like this:

HT2012                 1                       HT2012 
HT2012                 2                       VT2013
HT2012                 NA                      HT2013
HT2012                 NA                      VT2014
HT2012                 NA                      HT2014   
HT2012                 NA                      VT2015 
HT2012                 NA                      HT2015
HT2012                 NA                      VT2016
HT2012                 NA                      HT2016
HT2012                 NA                      VT2017   


[...]

HT2012                 1                       HT2013
HT2012                 2                       VT2014
HT2012                 NA                      VT2014

[...]    

Does this mean that an ifelse statement can't access values calculated by that same statement, and if so, how do we remedy this?

Excerpt of data:

structure(list(STARTPERIOD_KOD = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("HT2012", 
"HT2013", "HT2014", "HT2015", "HT2016", "HT2017", "HT2018", "HT2019"
), class = "factor"), PERIOD_I_ORDNING = structure(c(2L, 3L, 
4L, 5L, 6L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("-", 
"1", "2", "3", "4", "5", "6"), class = "factor"), PERIOD_KOD = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L), .Label = c("HT2012", "VT2013", "HT2013", "VT2014", "HT2014", 
"VT2015", "HT2015", "VT2016", "HT2016", "VT2017", "HT2017", "VT2018", 
"HT2018", "VT2019", "HT2019", "VT2020", "HT2020", "VT2021", "HT2021", 
"VT2022", "HT2022", "VT2023", "HT2023", "VT2024", "HT2024", "VT2025", 
"HT2025", "VT2026", "HT2026", "VT2027", "HT2027", "VT2028", "HT2028", 
"VT2029", "HT2029", "VT2030", "HT2030"), class = c("ordered", 
"factor"))), row.names = c(NA, 30L), class = "data.frame")

Aucun commentaire:

Enregistrer un commentaire