Skip to content

Commit

Permalink
Merge branch 'rc0.3.4-2'
Browse files Browse the repository at this point in the history
Conflicts: because of dev repo in README.md
  • Loading branch information
kaz-yos committed Mar 7, 2014
2 parents 15bf9a8 + ea3bf4e commit 2bab88c
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 81 deletions.
14 changes: 5 additions & 9 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
README.md

## Avoid these temporary scripts
clean_comments.sh
Makefile
compile.sh
man_create.R
man/Rd2roxygen.R

## Exclude TableOne related functions
## R/CreateTableOne.R
## R/summary.TableOne.R
## R/print.TableOne.R
## man/CreateTableOne.Rd
## man/summary.TableOne.Rd
## man/print.TableOne.Rd
man/clean_comments.sh
tableone.Rcheck
.*.tar.gz
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: tableone
Type: Package
Title: Create "Table 1" to describe baseline characteristics
Version: 0.3.3
Date: 2014-02-22
Version: 0.3.4
Date: 2014-03-04
Author: Kazuki Yoshida, Justin Bohn
Maintainer: Kazuki Yoshida <[email protected]>
Description: This package creates "Table 1", i.e., description of baseline
Expand Down
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Makefile for generating R packages.
## original by 2011 Andrew Redd
## https://gist.github.com/halpo/1374344

## Modified by kaz-yos for a different directory structure.
## To be put in the package directory.
## The build and check output files are put in the package directory.
## These files as well as Makefile have to be .Rbuildignore.
## writing rules
## https://www.gnu.org/software/make/manual/html_node/Rules.html#Rules

## Extract the package name and version from the DESCRIPTION file.
PKG_NAME=$(shell grep -i ^package: DESCRIPTION | cut -d : -d \ -f 2)
PKG_VERSION=$(shell grep -i ^version: DESCRIPTION | cut -d : -d \ -f 2)

## Define files
R_FILES := $(wildcard R/*.R)
SRC_FILES := $(wildcard src/*) $(addprefix src/, $(COPY_SRC))
PKG_FILES := DESCRIPTION NAMESPACE NEWS $(R_FILES) $(SRC_FILES)


## .PHONY to allow non-file targets (file targets should not be here)
## https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
.PHONY: build check install clean


### Define targets

## build depends on the *.tar.gz file, i.e., its own product.
## *.tar.gz file is defined seprately to prevent build execution on every invocation.
build: $(PKG_NAME)_$(PKG_VERSION).tar.gz

## (file target) The *.tar.gz file depends on package files including NAMESPACE,
## and build *.tar.gz file from these.
$(PKG_NAME)_$(PKG_VERSION).tar.gz: $(PKG_FILES)
R CMD build ../${PKG_NAME}

## (file target) NAMESPACE depends on *.R files, and excecute roxygen2 on these.
NAMESPACE: $(R_FILES)
Rscript -e "library(roxygen2); roxygenize('.')"

## check requires the *.tar.gz file, and execute strict tests on it.
check: $(PKG_NAME)_$(PKG_VERSION).tar.gz
R CMD check --as-cran ./$(PKG_NAME)_$(PKG_VERSION).tar.gz

## install requires the *.tar.gz file, and execute installation using it.
install: $(PKG_NAME)_$(PKG_VERSION).tar.gz
R CMD install ./$(PKG_NAME)_$(PKG_VERSION).tar.gz


## clean has no dependency, and execute removal of make output files.
clean:
-rm -f $(PKG_NAME)_*.tar.gz
-rm -r -f $(PKG_NAME).Rcheck
-rm -r -f man/*.Rd
-rm -r -f NAMESPACE


## Define a target "list" that just prints the names of files.
.PHONY: list
list:
@echo "R files:"
@echo $(R_FILES)
@echo
@echo "Source files:"
@echo $(SRC_FILES)
16 changes: 15 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
tableone 0.3.4 (2014-03-04)
----------------------------------------------------------------

BUG FIXES

* Added the Create* functions handling of all NA/NaN variables.
These invalid variables are examined at the beginning and dropped
with warning for safety.

* For the strata argument, variables with only one level is dropped
with warning because these are meaningless, and caused data
handling problems.


tableone 0.3.3 (2014-02-22)
----------------------------------------------------------------

Expand Down Expand Up @@ -91,7 +105,7 @@ BUG FIXES

* This version is to better conform to the CRAN standards.

* Documents are now included in
* Documents are now included in


tableone 0.1.0 (2014-02-08)
Expand Down
2 changes: 1 addition & 1 deletion R/CreateCatTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ CreateCatTable <-
dat[datNotFactor] <- lapply(dat[datNotFactor], factor)

## Create strata data frame (data frame with only strata variables)
strata <- ModuleReturnStrata(strata, data, dat)
strata <- ModuleReturnStrata(strata, data)


### Actual descriptive statistics are calculated here.
Expand Down
2 changes: 1 addition & 1 deletion R/CreateContTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ CreateContTable <-
test <- ModuleReturnFalseIfNoStrata(strata, test)

## Create strata data frame (data frame with only strata variables)
strata <- ModuleReturnStrata(strata, data, dat)
strata <- ModuleReturnStrata(strata, data)


## Handle non-numeric elements (intergers give TRUE, and pass)
Expand Down
9 changes: 8 additions & 1 deletion R/CreateTableOne.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ CreateTableOne <-
argsExact = argsExact
)
## Add strata = strata for argument only if strata is given
if(!missing(strata)) {
if (!missing(strata)) {

## Check strata. This returns a DF. Returns a "Overall" DF if strata is missing.
## Must not be place outside if (!missing(strata)) { }.
dfStrata <- ModuleReturnStrata(strata, data)
## Return variable names. Code inefficient in exchange for code simplicity.
strata <- names(dfStrata)

## Create lists of arguments including strata
argsCreateContTable <- c(list(strata = strata), argsCreateContTable)
argsCreateCatTable <- c(list(strata = strata), argsCreateCatTable)
}
Expand Down
79 changes: 65 additions & 14 deletions R/modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,34 @@ ModuleStopIfNotDataFrame <- function(data) {
}

## Extract variables that exist in the data frame
## Also exclude variables that only have NA
ModuleReturnVarsExist <- function(vars, data) {

## Check if variables exist. Drop them if not.
varsNotInData <- setdiff(vars, names(data))

if (length(varsNotInData) > 0) {
warning("The data frame does not have ",
warning("The data frame does not have: ",
paste0(varsNotInData, sep = " "), " Dropped")
## Only keep variables that exist
vars <- intersect(vars, names(data))
}
## Return existing variables

## Check if variables have at least some valid values (not NA/NaN)
logiAllNaVars <- sapply(X = data[vars],
FUN = function(VAR) {
all(is.na(VAR))
},
simplify = TRUE)

if (any(logiAllNaVars)) {
warning("These variables only have NA/NaN: ",
paste0(vars[logiAllNaVars], sep = " "), " Dropped")

vars <- vars[!logiAllNaVars]
}

## Return existing and valid variables
return(vars)
}

Expand All @@ -47,24 +63,59 @@ ModuleReturnFalseIfNoStrata <- function(strata, test) { # Give strata variable n
}

## Check statra variables and conditionally create strata data frame
ModuleReturnStrata <- function(strata, data, dat) { # Give strata variable names
ModuleReturnStrata <- function(strata, data) { # Give strata variable names
## strata: char vector; data: data frame given

if(missing(strata)) {
## If there is no strata, give "Overall" to every subject
strata <- rep("Overall", dim(dat)[1]) # Check if dim(dat)[[1]] is correct.
} else {
## If there is no strata, give "Overall" to every subject (dim1 is nrow)
strata <- rep("Overall", nrow(data))

} else { # If strata is given

## Check presence of strata variables in the data frame (multivariable support)
presenceOfStrata <- strata %in% names(data)
## Delete variables that do not exist in the data frame
strata <- strata[presenceOfStrata]
## unique it first to remove duplications
strata <- unique(strata)

## Drop nonexisting and NA/NaN only variables
strata <- ModuleReturnVarsExist(strata, data)

## Conditional on presence of remaining variable
if (length(strata) == 0) {
stop("None of the stratifying variables are present in the data frame")
}
## Stop if none left
stop("None of the stratifying variables are present in the data frame.")

} else {

## Check validity of the remaining variables
logiSingleLevelOnly <-
lapply(data[c(strata)],
function(VEC) {
## Check number of levels
nLevels <- ifelse(test = is.factor(VEC),
yes = nlevels(VEC),
no = nlevels(factor(VEC)))
## Return logical indicating only one valid level
nLevels == 1
})
logiSingleLevelOnly <- unlist(logiSingleLevelOnly)

## Only keep variables that have 2+ levels
if (any(logiSingleLevelOnly)) {
warning("These variables have only one valid level: ",
paste0(strata[logiSingleLevelOnly], sep = " "), " Dropped")

strata <- strata[!logiSingleLevelOnly]

}

## Extract the stratifying variable vector (strata is a data frame)
strata <- data[c(strata)]
## Stop if no variables are left
if (length(strata) == 0) {
## Stop if none left
stop("None of the stratifying variables have 2+ valid levels.")
}

## Extract the stratifying variable vector (strata is a data frame)
strata <- data[c(strata)]
}
}

## return DF with strata variable(s)
Expand Down
52 changes: 25 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ See also the demonstration here: http://rpubs.com/kaz_yos/tableone-demo-e
+ exact = c("status","stage"), cramVars = "sex")
Stratified by trt
1 2 p test
n 158 154
time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
status (%) 0.884 exact
0 83 (52.5) 85 (55.2)
1 10 ( 6.3) 9 ( 5.8)
2 65 (41.1) 60 (39.0)
age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018
sex = m/f (%) 21/137 (13.3/86.7) 15/139 (9.7/90.3) 0.421
ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567
hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088
spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985
edema (%) 0.877
0 132 (83.5) 131 (85.1)
0.5 16 (10.1) 13 ( 8.4)
1 10 ( 6.3) 10 ( 6.5)
1 2 p test
n 158 154
time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
status (%) 0.884 exact
0 83 (52.5) 85 (55.2)
1 10 ( 6.3) 9 ( 5.8)
2 65 (41.1) 60 (39.0)
age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018
sex = m/f (%) 21/137 (13.3/86.7) 15/139 (9.7/90.3) 0.421
ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567
hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088
spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985
edema (%) 0.877
0 132 (83.5) 131 (85.1)
0.5 16 (10.1) 13 ( 8.4)
1 10 ( 6.3) 10 ( 6.5)
bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm
chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm
albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874
albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874
copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm
alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm
ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460
ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460
trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm
platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555
protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197
stage (%) 0.205 exact
1 12 ( 7.6) 4 ( 2.6)
2 35 (22.2) 32 (20.8)
3 56 (35.4) 64 (41.6)
4 55 (34.8) 54 (35.1)
platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555
protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197
stage (%) 0.205 exact
1 12 ( 7.6) 4 ( 2.6)
2 35 (22.2) 32 (20.8)
3 56 (35.4) 64 (41.6)
4 55 (34.8) 54 (35.1)
```


Expand All @@ -60,8 +60,6 @@ This is the release version of the tableone package either to be released on CRA
- http://cran.r-project.org/web/packages/tableone/index.html




If you prefer to follow the latest development, please see the developmetal repo:

- https://github.com/kaz-yos/tableone/tree/develop
25 changes: 0 additions & 25 deletions man_create.R

This file was deleted.

0 comments on commit 2bab88c

Please sign in to comment.