basic_ts.R 1.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#' basicr

#' basicr
#' @param df tbl_friendlyts data.frame
#' @param target target
#' @param group "none","YEAR","MONTH","WDAY","HOUR","MINUTE","SECOND" or several
#' @param precision "daily","hourly","minute","second",...
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' basicr()
#' }
basic_ts <- function(df,target,group,precision){

  basic <- basic_info(df=df,target=target,group=group)
  varnames <- setdiff(names(basic),group)

  varnames_ts <- c(friendlyts::get_name_date_col(precision=precision),"value",varnames)
  res_ts <- as.data.frame(setnames(replicate(length(varnames_ts),rep(NA,nrow(df)), simplify = F), varnames_ts))
  res_ts[,friendlyts::get_name_date_col(precision=precision)] <- as.data.frame(df)[,friendlyts::get_name_date_col(precision=precision)]

  for (i in 1:nrow(basic)) {

    tmp <- lapply(group,function(x){
      res <- which(as.numeric(basic[i,x]) == as.numeric(res_ts[,x]))
      return(res)
    })

    tmp <- table(unlist(tmp))
    indice <-  as.numeric(names(tmp[tmp==length(group)]))
    res_ts[indice,varnames] <-basic[i,varnames]
  }

36
37
  res_ts[,"value"] <-df[,target]

38
39
40
41
42
  res <- friendlyts::friendlyts(df=res_ts,date_type="friendlyts",precision=precision)
  class(res) <- c("basic_ts",class(res))
  return(res)

}