basicr.R 2.03 KB
Newer Older
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
1
2
3
4

#' basicr

#' basicr
jml's avatar
jml committed
5
#' @param df tbl_friendlyts data.frame
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
6
#' @param target target
jml's avatar
jml committed
7
#' @param group "none","YEAR","MONTH","WDAY","HOUR","MINUTE","SECOND"
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
8
9
10
11
12
13
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' basicr()
#' }
jml's avatar
jml committed
14
basicr <- function(df,target,group="none"){
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
15

jml's avatar
jml committed
16
17
  if (is.null(df) || (!inherits(df,"tbl_friendlyts")) ) {
    stop("need non-null tbl_friendlyts df")
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
18
19
20
  }

  res <- switch(group,
jml's avatar
jml committed
21
22
23
    "none"  = df %>% basic_(target=target),
    "YEAR"  = df %>% group_by(YEAR) %>% basic_(target=target),
    "MONTH" = df %>% group_by(MONTH) %>% basic_(target=target),
jml's avatar
typo    
jml committed
24
    "WDAY"  = df %>% group_by(WDAY) %>% basic_(target=target),
jml's avatar
jml committed
25
26
27
    "HOUR"  = df %>% group_by(HOUR) %>% basic_(target=target),
    "MINUTE"  = df %>% group_by(MINUTE) %>% basic_(target=target),
    "SECOND"  = df %>% group_by(SECOND) %>% basic_(target=target),
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
28
29
    stop("group not recognized"))

Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
30
  class(res) <- class(res)[-which(class(res)=="tbl_friendlyts")]
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
31
32
33
34
35
36
37
38
  res <- as_tbl_basic(res,group)

  return(res)
}

#' basic_

#' basic_
jml's avatar
jml committed
39
#' @param df tbl_friendlyts object
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
40
41
42
43
44
45
46
#' @param target character
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' basic_()
#' }
jml's avatar
jml committed
47
basic_ <- function(df,target){
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
48

jml's avatar
jml committed
49
  names(df)[names(df)==target] <- "targ"
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
50

jml's avatar
jml committed
51
  res <- df %>% summarise(
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
      n = n(),
      q0  = as.numeric(stats::quantile(targ,probs=0,na.rm=TRUE)),
      q1  = as.numeric(stats::quantile(targ,probs=0.25,na.rm=TRUE)),
      q2  = as.numeric(stats::quantile(targ,probs=0.50,na.rm=TRUE)),
      q3  = as.numeric(stats::quantile(targ,probs=0.75,na.rm=TRUE)),
      q4  = as.numeric(stats::quantile(targ,probs=1,na.rm=TRUE)),
      IQR = as.numeric(stats::quantile(targ,probs=0.75,na.rm=TRUE)) - as.numeric(stats::quantile(targ,probs=0.25,na.rm=TRUE)),
      m   = mean(targ, na.rm = TRUE),
      sd  = stats::sd(targ, na.rm = TRUE),
      cv  = stats::sd(targ, na.rm = TRUE)/mean(targ, na.rm = TRUE)
  )

  indice <- which(is.na(res$m))
  if (length(indice)>0) {
    res$m[indice] <- mean(res$m,na.rm=TRUE)
    res$sd[indice] <- mean(res$sd,na.rm=TRUE)
    return(res)

  } else {
    return(res)
  }

}