library(tidyverse)
library(magrittr)
library(lubridate)
rm(list = ls())
time_dat <- c("2016.01.01", "2015.07.21", "2015.03.01", "2015.01.01", "2014.11.15",
"2014.10.28", "2014.10.11", "2014.10.01", "2014.09.28", "2014.01.01", "2013.12.01",
"2013.06.12", "2013.04.26", "2012.11.03")
# remove day (make same day)
time_dat <- time_dat %>% ymd() %>% format("%Y-%m-01") %>% ymd()
print(time_dat)
## [1] "2016-01-01" "2015-07-01" "2015-03-01" "2015-01-01" "2014-11-01"
## [6] "2014-10-01" "2014-10-01" "2014-10-01" "2014-09-01" "2014-01-01"
## [11] "2013-12-01" "2013-06-01" "2013-04-01" "2012-11-01"
value_dat <- c("B", "B", rep("A", 5), rep("C", 5), "B", "B") %>% as.factor()
print(value_dat)
## [1] B B A A A A A C C C C C B B
## Levels: A B C
dat <- data_frame(time = time_dat, value = value_dat)
# make unique identifier
new_value <- rep("S1", length(dat$value))
idx <- 1
current_string <- dat$value[1]
for (i in 2:length(dat$value)) {
if (current_string != dat$value[i]) {
current_string <- dat$value[i]
idx <- idx + 1
}
new_value[i] <- paste0("S", idx)
}
dat <- dat %>% mutate(new_value = as.factor(new_value))
knitr::kable(dat)
| 2016-01-01 |
B |
S1 |
| 2015-07-01 |
B |
S1 |
| 2015-03-01 |
A |
S2 |
| 2015-01-01 |
A |
S2 |
| 2014-11-01 |
A |
S2 |
| 2014-10-01 |
A |
S2 |
| 2014-10-01 |
A |
S2 |
| 2014-10-01 |
C |
S3 |
| 2014-09-01 |
C |
S3 |
| 2014-01-01 |
C |
S3 |
| 2013-12-01 |
C |
S3 |
| 2013-06-01 |
C |
S3 |
| 2013-04-01 |
B |
S4 |
| 2012-11-01 |
B |
S4 |
new_dat <- dat %>% arrange(new_value, time)
knitr::kable(new_dat)
| 2015-07-01 |
B |
S1 |
| 2016-01-01 |
B |
S1 |
| 2014-10-01 |
A |
S2 |
| 2014-10-01 |
A |
S2 |
| 2014-11-01 |
A |
S2 |
| 2015-01-01 |
A |
S2 |
| 2015-03-01 |
A |
S2 |
| 2013-06-01 |
C |
S3 |
| 2013-12-01 |
C |
S3 |
| 2014-01-01 |
C |
S3 |
| 2014-09-01 |
C |
S3 |
| 2014-10-01 |
C |
S3 |
| 2012-11-01 |
B |
S4 |
| 2013-04-01 |
B |
S4 |
Finally, we will group by the data frame by factor (i.e., A,b,c,..), and then calculate month difference. (see the interval = ~ code below).
final_dat <- new_dat %>% group_by(new_value) %>% summarize(interval = (year(max(time)) *
12 + month(max(time))) - (year(min(time)) * 12 + month(min(time))), value = value[1])
knitr::kable(final_dat)
| S1 |
6 |
B |
| S2 |
5 |
A |
| S3 |
16 |
C |
| S4 |
5 |
B |
# library(forcats)
# aa<-final_dat%>%mutate(value=fct_relevel(value,c('B','A','C')))%>%arrange(value)
# knitr::kable(aa)