knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
In a study, most of the tables are standard descriptive table. Some times tables will be grouped by population stratified by treatment group. Further in cancer researches, the variables repeated by cycle. This package makes it fairly straightforward to produce such a table using R. The table output is a matrix and with other function can output as a flextable object. The advantage of the flextble
object is, it supports HTML output and the word output. This make it easy to write a report with Rmakrdown and output a word report with officedown. It also made it easy to deal with macro data, with some attributes works just like SAS.
# library(cttab) devtools::load_all()
Assume you have data (test_data_csv.csv
) under test_raw
folder, along with CLU (test_data_csv_CLU.csv
) and DLU (test_data_csv_DLU.csv
) file. You can load the data easily as the following, and the date will be converted automatically with %d%m%Y
format. The data meta data, CLU and DLU, will be loaded as well.
dt <- read_macro(path = "data_raw", file_name = "test_data_csv.csv") str(dt[,c(5:13)])
As you can find out, all the variable has label and value label attributes, and the date imported with date format. This is imported from DLU and CLU file, like label and format in SAS. Variable label can be assigned with var_lab
function. A value label can be assigned with val_lab
function with a named atomic vector, name as label of the value.
data(mtcars) mtcars <- mtcars[, c("mpg", "am")] str(mtcars) # Add variable label var_lab(mtcars$mpg) <- "Miles/(US) gallon" var_lab(mtcars$am) <- "Transmission" # Add value label val_lab(mtcars$am) <- c("Automatic" = 0, "Manual"=1) str(mtcars)
If we want to create a frequency table for Transmission, we can use table
function directly or convert this variable to character
table(mtcars$am) table(as.character(mtcars$am))
But by doing so, we will lost variable label and may take advantage of the value label. So we will use to_character
in this example. Other functions like to_factor
and to_numeric
is provide to keep variable labels. The to_character
function will assign the unique non-missing and non-blank value as value label. The to_factor
function will apply the value label to the variable and convert it to factor.
str(as.character(mtcars$am)) # variable and value label dropped str(to_character(mtcars$am)) # all kept str(to_factor(mtcars$am)) # value label applied and converted to factor table(mtcars$am) table(to_factor(mtcars$am))
Note at the last one, this is the advantage of value label.
For the example we use the macro data. Some variables might be a subset of other variables, and we may not get exact frequency table if summarize as whole. In the following situation, only for the subjects with tumor grade recorded (variable TUMGRADE
) has values for tumor grade PTM (only showed P NOTTSCOREP
in this example). And the subset can be dong with select
parameters, more variables can be given with named atomic vector. But before that, as NOTTSCOREP
is numeric in DLU and we want to change it to character. If we use as.character
function, we will lost label attributes as stated earlier. We use to_character
function here for the conversion.
# Only total dt$NOTTSCOREP <- to_character(dt$NOTTSCOREP) # Convert to character res1 <- tab1(vars = c("SEX", "ARM", "HEIGHT", "WEIGHT", "SIDE", "TUBULAR", "TUMGRADE", "NOTTSCOREP"), data = dt[!is.na(dt$REGDAT), ], select = c("NOTTSCOREP" = "TUMGRADE == 1")) write_table(res1)
And we want to summarize table by treatment arm in the following example.
res2 <- tab1(vars = c("SEX", "HEIGHT", "WEIGHT", "SIDE", "TUBULAR", "TUMGRADE", "NOTTSCOREP"), group = "ARM", data = dt[!is.na(dt$REGDAT), ], select = c("NOTTSCOREP" = "TUMGRADE == 1")) write_table(res2)
For some repeated measures, we want to describe data by time, cycle in cancer example. The repeated variable will automatically have shaded background.
# With cycle res3 <- tab1(vars = c("BLDTIMPT", "HAEMANAL1", "NEUTROPHILS1", "LYMPHOCYTES1", "HB", "WBC", "PLTS", "BIOCHEMANAL1", "ECOGPERSTATUS"), group = "ARM", row_split = "BLDRESCYCLENUM", data = dt[!is.na(dt$BLDRESCYCLENUM), ]) write_table(res3, caption = c("Table 1. Description by Arm and Cycle", "ASAT population"))
Can perform data listing just with write_table
function.
write_table(head(dt), caption = c("Table 2. Data Listing"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.