basicr.R 2.17 KB
Newer Older
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
1

2
#' basic_info
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
3

4
#' basic_info
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
7
#' @param group "none","YEAR","MONTH","WDAY","HOUR","MINUTE","SECOND" or several
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
8
9
10
11
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
12
#' basic_info()
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
13
#' }
14
basic_info <- 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
  if ( (length(group)>=1) && (prod(group %in% c("YEAR","MONTH","WDAY","HOUR","MINUTE","SECOND"))==1) && (sum(duplicated(group),na.rm=TRUE)==0) ){
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
21

22
    res <- df %>% group_by_(.dots=group) %>% basic_(target=target)
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
23

24
25
26
27
28
29
30
31
  } else if ((length(group)==1) && (group =="none")) {

    res <- df %>% basic_(target=target)

  } else stop("group must refer to: 'none', 'YEAR', 'MONTH', 'WDAY', 'HOUR','MINUTE','SECOND'")

  res <- tibble::as_tibble(res)
  class(res) <- c(class(res),"tbl_basic")
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
32
33
34
35
36
37
  return(res)
}

#' basic_

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

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

jml's avatar
jml committed
50
  res <- df %>% summarise(
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
51
      n = n(),
52
      n_na    = as.numeric(length(which(is.na(targ)))),
53
54
55
56
57
58
59
      q_0     = as.numeric(stats::quantile(targ,probs=0,na.rm=TRUE)),
      q_0_05  = as.numeric(stats::quantile(targ,probs=0.05,na.rm=TRUE)),
      q_0_25  = as.numeric(stats::quantile(targ,probs=0.25,na.rm=TRUE)),
      q_0_5   = as.numeric(stats::quantile(targ,probs=0.50,na.rm=TRUE)),
      q_0_75  = as.numeric(stats::quantile(targ,probs=0.75,na.rm=TRUE)),
      q_0_95  = as.numeric(stats::quantile(targ,probs=0.95,na.rm=TRUE)),
      q_1  = as.numeric(stats::quantile(targ,probs=1,na.rm=TRUE)),
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
      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)
  }

}