Cut variables, if breaks are given these are used, otherwise cuts into using group size given by probs, or equispace groups on range. Default is equally sized groups if possible

dcut(
  data,
  y = NULL,
  x = NULL,
  breaks = 4,
  probs = NULL,
  equi = FALSE,
  regex = mets.options()$regex,
  sep = NULL,
  na.rm = TRUE,
  labels = NULL,
  all = FALSE,
  ...
)

Arguments

data

if x is formula or names for data frame then data frame is needed.

y

name of variable, or fomula, or names of variables on data frame.

x

name of variable, or fomula, or names of variables on data frame.

breaks

number of breaks, for variables or vector of break points,

probs

groups defined from quantiles

equi

for equi-spaced breaks

regex

for regular expressions.

sep

seperator for naming of cut names.

na.rm

to remove NA for grouping variables.

labels

to use for cut groups

all

to do all variables, even when breaks are not unique

...

Optional additional arguments

Author

Klaus K. Holst and Thomas Scheike

Examples

data("sTRACE",package="timereg")
sTRACE$age2 <- sTRACE$age^2
sTRACE$age3 <- sTRACE$age^3

mm <- dcut(sTRACE,~age+wmi)
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4
#> X1944 (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]

mm <- dcut(sTRACE,catage4+wmi4~age+wmi)
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>         catage4      wmi4
#> X1944 (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]

mm <- dcut(sTRACE,~age+wmi,breaks=c(2,4))
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>          agecat.2  wmicat.4
#> X1944 (68.7,92.1] (1.4,2.7]
#> X5783 (68.7,92.1] (1.4,2.7]
#> X784  (68.7,92.1] [0.4,1.4]
#> X3763 [28.1,68.7] [0.4,1.4]
#> X2927 [28.1,68.7] (1.4,2.7]
#> X4511 [28.1,68.7] [0.4,1.4]

mm <- dcut(sTRACE,c("age","wmi"))
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4
#> X1944 (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]

mm <- dcut(sTRACE,~.)
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>                   nocat.4  wmicat.4  agecat.4       timecat.4
#> X1944 (1.51e+03,3.44e+03] (1.4,1.8] (76,92.1] [0.000687,1.47]
#> X5783 (5.18e+03,6.64e+03] (1.8,2.7] (68.7,76]     (6.81,8.16]
#> X784        [22,1.51e+03] [0.4,1.1] (76,92.1] [0.000687,1.47]
#> X3763 (3.44e+03,5.18e+03] (1.1,1.4] [28.1,60]     (6.81,8.16]
#> X2927 (1.51e+03,3.44e+03] (1.4,1.8] (60,68.7]     (6.81,8.16]
#> X4511 (3.44e+03,5.18e+03] [0.4,1.1] (60,68.7]     (1.47,6.05]
#>                 age2cat.4           age3cat.4
#> X1944 (5.77e+03,8.48e+03] (4.39e+05,7.81e+05]
#> X5783 (4.71e+03,5.77e+03] (3.24e+05,4.39e+05]
#> X784  (5.77e+03,8.48e+03] (4.39e+05,7.81e+05]
#> X3763       [788,3.6e+03] [2.21e+04,2.16e+05]
#> X2927  (3.6e+03,4.71e+03] (2.16e+05,3.24e+05]
#> X4511  (3.6e+03,4.71e+03] (2.16e+05,3.24e+05]

mm <- dcut(sTRACE,c("age","wmi"),breaks=c(2,4))
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>          agecat.2  wmicat.4
#> X1944 (68.7,92.1] (1.4,2.7]
#> X5783 (68.7,92.1] (1.4,2.7]
#> X784  (68.7,92.1] [0.4,1.4]
#> X3763 [28.1,68.7] [0.4,1.4]
#> X2927 [28.1,68.7] (1.4,2.7]
#> X4511 [28.1,68.7] [0.4,1.4]

gx <- dcut(sTRACE$age)
head(gx)
#> [1] (76,92.1] (68.7,76] (76,92.1] [28.1,60] (60,68.7] (60,68.7]
#> Levels: [28.1,60] (60,68.7] (68.7,76] (76,92.1]


## Removes all cuts variables with these names wildcards
mm1 <- drm(mm,c("*.2","*.4"))
head(mm1)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4

## wildcards, for age, age2, age4 and wmi
head(dcut(mm,c("a*","?m*")))
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>          agecat.2  wmicat.4  agecat.4           age2cat.4           age3cat.4
#> X1944 (68.7,92.1] (1.4,1.8] (76,92.1] (5.77e+03,8.48e+03] (4.39e+05,7.81e+05]
#> X5783 (68.7,92.1] (1.8,2.7] (68.7,76] (4.71e+03,5.77e+03] (3.24e+05,4.39e+05]
#> X784  (68.7,92.1] [0.4,1.1] (76,92.1] (5.77e+03,8.48e+03] (4.39e+05,7.81e+05]
#> X3763 [28.1,68.7] (1.1,1.4] [28.1,60]       [788,3.6e+03] [2.21e+04,2.16e+05]
#> X2927 [28.1,68.7] (1.4,1.8] (60,68.7]  (3.6e+03,4.71e+03] (2.16e+05,3.24e+05]
#> X4511 [28.1,68.7] [0.4,1.1] (60,68.7]  (3.6e+03,4.71e+03] (2.16e+05,3.24e+05]

## with direct asignment
drm(mm) <- c("*.2","*.4")
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4

dcut(mm) <- c("age","*m*")
dcut(mm) <- ageg1+wmig1~age+wmi
head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4       timecat.4     ageg1     wmig1
#> X1944 (76,92.1] (1.4,1.8] [0.000687,1.47] (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]     (6.81,8.16] (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1] [0.000687,1.47] (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]     (6.81,8.16] [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]     (6.81,8.16] (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]     (1.47,6.05] (60,68.7] [0.4,1.1]

############################
## renaming
############################

head(mm)
#>         no wmi status chf    age sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4       timecat.4     ageg1     wmig1
#> X1944 (76,92.1] (1.4,1.8] [0.000687,1.47] (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]     (6.81,8.16] (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1] [0.000687,1.47] (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]     (6.81,8.16] [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]     (6.81,8.16] (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]     (1.47,6.05] (60,68.7] [0.4,1.1]
drename(mm, ~Age+Wmi) <- c("wmi","age")
head(mm)
#>         no Age status chf    Wmi sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4       timecat.4     ageg1     wmig1
#> X1944 (76,92.1] (1.4,1.8] [0.000687,1.47] (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]     (6.81,8.16] (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1] [0.000687,1.47] (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]     (6.81,8.16] [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]     (6.81,8.16] (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]     (1.47,6.05] (60,68.7] [0.4,1.1]
mm1 <- mm

## all names to lower
drename(mm1) <- ~.
head(mm1)
#>         no age status chf    wmi sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4       timecat.4     ageg1     wmig1
#> X1944 (76,92.1] (1.4,1.8] [0.000687,1.47] (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]     (6.81,8.16] (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1] [0.000687,1.47] (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]     (6.81,8.16] [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]     (6.81,8.16] (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]     (1.47,6.05] (60,68.7] [0.4,1.1]

## A* to lower
mm2 <-  drename(mm,c("A*","W*"))
head(mm2)
#>         no age status chf    wmi sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4       timecat.4     ageg1     wmig1
#> X1944 (76,92.1] (1.4,1.8] [0.000687,1.47] (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]     (6.81,8.16] (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1] [0.000687,1.47] (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]     (6.81,8.16] [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]     (6.81,8.16] (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]     (1.47,6.05] (60,68.7] [0.4,1.1]
drename(mm) <- "A*"
head(mm)
#>         no age status chf    Wmi sex diabetes     time vf     age2     age3
#> X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 7212.086 612479.2
#> X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 5504.601 408402.9
#> X784   784 0.8      9   0 78.081   0        1 0.196000  0 6096.643 476031.9
#> X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 3077.919 170759.9
#> X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 3968.622 250011.3
#> X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 4575.711 309519.4
#>        agecat.4  wmicat.4       timecat.4     ageg1     wmig1
#> X1944 (76,92.1] (1.4,1.8] [0.000687,1.47] (76,92.1] (1.4,1.8]
#> X5783 (68.7,76] (1.8,2.7]     (6.81,8.16] (68.7,76] (1.8,2.7]
#> X784  (76,92.1] [0.4,1.1] [0.000687,1.47] (76,92.1] [0.4,1.1]
#> X3763 [28.1,60] (1.1,1.4]     (6.81,8.16] [28.1,60] (1.1,1.4]
#> X2927 (60,68.7] (1.4,1.8]     (6.81,8.16] (60,68.7] (1.4,1.8]
#> X4511 (60,68.7] [0.4,1.1]     (1.47,6.05] (60,68.7] [0.4,1.1]

dd <- data.frame(A_1=1:2,B_1=1:2)
funn <- function(x) gsub("_",".",x)
drename(dd) <- ~.
drename(dd,fun=funn) <- ~.
names(dd)
#> [1] "a.1" "b.1"