Commit 73255f63 authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

put on gitlab

parents
^.*\.Rproj$
^\.Rproj\.user$
.Rproj.user
.Rhistory
.RData
Package: basicr
Type: Package
Version: 0.0.1
Authors@R: c(person("Jean-Marie", "Lepioufle", , "jml@nilu.no", role=c("aut","cre")),
person("NILU",role="cph"))
Title: basicr
Description: Compute basic statistics on tbl_friendlyts object.
- Mean, standard-deviation, IQR. min, max,...
- On different groups: hour,wday, month
Depends:
R (>= 3.4.2)
Imports:
tibble,
dplyr,
ggplot2,
grDevices
License: GPL-3
Encoding: UTF-8
LazyData: false
RoxygenNote: 5.0.1
# Generated by roxygen2: do not edit by hand
S3method(plot,tbl_basic)
export(as_tbl_basic)
export(basic_)
export(basicr)
export(plot_tbl_basic)
importFrom(dplyr,"%>%")
importFrom(dplyr,do)
importFrom(dplyr,group_by)
importFrom(dplyr,n)
importFrom(dplyr,summarise)
importFrom(dplyr,transmute_at)
#' as_tbl_basic
#' as_tbl_basic
#' @param df data.frame
#' @param group group character
#' @keywords diagnr
#' @export
#' @examples
#' \dontrun{
#' as_tbl_basic()
#' }
as_tbl_basic <- function(df,group){
res <- tibble::as_tibble(df)
names(res) <- switch(group,
"none" = c("n","q0","q1","q2","q3","q4","IQR","m","sd","cv"),
"HOUR" = c("HOUR" ,"n","q0","q1","q2","q3","q4","IQR","m","sd","cv"),
"WDAY" = c("WDAY" ,"n","q0","q1","q2","q3","q4","IQR","m","sd","cv"),
"MONTH" = c("MONTH","n","q0","q1","q2","q3","q4","IQR","m","sd","cv"),
stop("group not recognized"))
if (group=="HOUR") {res$HOUR <- factor(res$HOUR, levels = res$HOUR)}
if (group=="WDAY") {res$WDAY <- factor(res$WDAY, levels = res$WDAY)}
if (group=="MONTH"){res$MONTH <- factor(res$MONTH, levels = res$MONTH)}
class(res) <- c("tbl_basic",class(res))
return(res)
}
#' basicr
#' basicr
#' @param dataset tbl_friendlyts object
#' @param target target
#' @param group group character
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' basicr()
#' }
basicr <- function(dataset,target,group="none"){
if (is.null(dataset) || (!inherits(dataset,"tbl_friendlyts")) ) {
stop("need non-null tbl_friendlyts dataset")
}
res <- switch(group,
"none" = dataset %>% basic_(target=target),
"HOUR" = dataset %>% group_by(HOUR) %>% basic_(target=target),
"WDAY" = dataset %>% group_by(wday) %>% basic_(target=target),
"MONTH" = dataset %>% group_by(MONTH) %>% basic_(target=target),
stop("group not recognized"))
res <- as_tbl_basic(res,group)
return(res)
}
#' basic_
#' basic_
#' @param dataset tbl_friendlyts object
#' @param target character
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' basic_()
#' }
basic_ <- function(dataset,target){
if (is.null(dataset) || (!inherits(dataset,"tbl_friendlyts")) ) {
stop("need non-null tbl_friendlyts dataset")
}
names(dataset)[names(dataset)==target] <- "targ"
res <- dataset %>% summarise(
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)
}
}
#' plot_tbl_basic
#' plot_tbl_basic
#' @param x tbl_basic object
#' @param dataset friendlyr data.frame
#' @param target target
#' @param group group character
#' @param path path
#' @param name name
#' @param width_mm width_mm
#' @param height_mm height_mm
#' @keywords diagnr
#' @export
#' @examples
#' \dontrun{
#' plot_tbl_basic()
#' }
plot_tbl_basic <- function(x,dataset,target,group,path=NULL,name=NULL,width_mm=200,height_mm=200){
if (!is.null(path)) {
diagnrdir <- normalizePath(file.path(path,"diagnr","figures"),mustWork = FALSE)
dir.create(diagnrdir, showWarnings = FALSE, recursive = TRUE)
if(is.null(name)) {
filename <- normalizePath(tempfile(pattern = "file", tmpdir = diagnrdir, fileext = ".tiff"),mustWork = FALSE)
} else filename <- normalizePath(file.path(diagnrdir,paste0(name,".tif")),mustWork = FALSE)
grDevices::tiff(filename = filename, width = width_mm, height = height_mm,
units = "mm", pointsize = 12,
compression = "lzw",
bg = "transparent", res = 300)
} else grDevices::dev.new()
# just to get class
x <- x
names(dataset)[names(dataset)==target] <- "targ"
p <- switch(group,
"HOUR" = ggplot2::ggplot(dataset) + ggplot2::geom_boxplot(ggplot2::aes(x = HOUR, y = targ)) + ggplot2::labs(x = group, y = target),
"WDAY" = ggplot2::ggplot(dataset) + ggplot2::geom_boxplot(ggplot2::aes(x = wday, y = targ)) + ggplot2::labs(x = group, y = target),
"MONTH" = ggplot2::ggplot(dataset) + ggplot2::geom_boxplot(ggplot2::aes(x = MONTH, y = targ)) + ggplot2::labs(x = group, y = target),
stop("group not recognized"))
print(p)
if (!is.null(path)) {
grDevices::dev.off()
} else {}
return(TRUE)
}
#' @importFrom dplyr %>% group_by summarise n do transmute_at
NULL
#' @export
plot.tbl_basic <- function(x,...){
plot_tbl_basic(x,...)
}
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
Encoding: UTF-8
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/as_tbl_basic.R
\name{as_tbl_basic}
\alias{as_tbl_basic}
\title{as_tbl_basic
as_tbl_basic}
\usage{
as_tbl_basic(df, group)
}
\arguments{
\item{df}{data.frame}
\item{group}{group character}
}
\description{
as_tbl_basic
as_tbl_basic
}
\examples{
\dontrun{
as_tbl_basic()
}
}
\keyword{diagnr}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/basicr.R
\name{basic_}
\alias{basic_}
\title{basic_
basic_}
\usage{
basic_(dataset, target)
}
\arguments{
\item{dataset}{tbl_friendlyts object}
\item{target}{character}
}
\description{
basic_
basic_
}
\examples{
\dontrun{
basic_()
}
}
\keyword{basicr}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/basicr.R
\name{basicr}
\alias{basicr}
\title{basicr
basicr}
\usage{
basicr(dataset, target, group = "none")
}
\arguments{
\item{dataset}{tbl_friendlyts object}
\item{target}{target}
\item{group}{group character}
}
\description{
basicr
basicr
}
\examples{
\dontrun{
basicr()
}
}
\keyword{basicr}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot.R
\name{plot_tbl_basic}
\alias{plot_tbl_basic}
\title{plot_tbl_basic
plot_tbl_basic}
\usage{
plot_tbl_basic(x, dataset, target, group, path = NULL, name = NULL,
width_mm = 200, height_mm = 200)
}
\arguments{
\item{x}{tbl_basic object}
\item{dataset}{friendlyr data.frame}
\item{target}{target}
\item{group}{group character}
\item{path}{path}
\item{name}{name}
\item{width_mm}{width_mm}
\item{height_mm}{height_mm}
}
\description{
plot_tbl_basic
plot_tbl_basic
}
\examples{
\dontrun{
plot_tbl_basic()
}
}
\keyword{diagnr}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment