Skip to contents
loading...

Create a data frame (base's data.frame, data.table or tibble's tbl_df)

Usage

dtx(..., .name_repair = c("check_unique", "unique", "universal", "minimal"))

dtbl(..., .name_repair = c("check_unique", "unique", "universal", "minimal"))

dtf(..., .name_repair = c("check_unique", "unique", "universal", "minimal"))

dtt(..., .name_repair = c("check_unique", "unique", "universal", "minimal"))

Arguments

...

A set of name-value pairs. The content of the data frame. See tibble() for more details on the way dynamic-dots are processed.

.name_repair

The way problematic column names are treated, see also tibble() for details.

Value

A data frame as a tbl_df object for dtbl(), a data.frame for dtf() and a data.table for dtt().

Note

data.table and tibble's tbl_df do no use row names. However, you can add a column named .rownames(by default), or the name that is in getOption("SciViews.dtx.rownames") and it will be automatically set as row names when the object is converted into a data.frame with as_dtf(). For dtf(), just create a column of this name and it is directly used as row names for the resulting data.frame object.

Examples

dtbl1 <- dtbl(
  x = 1:5,
  y = rnorm(5),
  f = letters[1:5],
  l = sample(c(TRUE, FALSE), 5, replace = TRUE)
)
class(dtbl1)
#> [1] "tbl_df"     "tbl"        "data.frame"

dtf1 <- dtf(
  x = 1:5,
  y = rnorm(5),
  f = letters[1:5],
  l = sample(c(TRUE, FALSE), 5, replace = TRUE)
)
class(dtf1)
#> [1] "data.frame"

dtt1 <- dtt(
  x = 1:5,
  y = rnorm(5),
  f = letters[1:5],
  l = sample(c(TRUE, FALSE), 5, replace = TRUE))
class(dtt1)
#> [1] "data.table" "data.frame"

# Using dtx(), one construct the preferred data frame object
# (a data.table by default, can be changed with options(SciViews.as_dtx = ...))
dtx1 <- dtx(
  x = 1:5,
  y = rnorm(5),
  f = letters[1:5],
  l = sample(c(TRUE, FALSE), 5, replace = TRUE))
class(dtx1) # data.table by default
#> [1] "data.table" "data.frame"

# With svBase data.table and data.frame objects have the same nice print as tibbles
dtbl1
#> # A tibble: 5 × 4
#>       x        y f     l    
#>   <int>    <dbl> <chr> <lgl>
#> 1     1 -0.171   a     TRUE 
#> 2     2  1.63    b     FALSE
#> 3     3 -0.783   c     FALSE
#> 4     4 -0.00289 d     TRUE 
#> 5     5  0.413   e     FALSE
dtf1
#>   x           y f     l
#> 1 1 -0.09744510 a  TRUE
#> 2 2 -0.93584735 b  TRUE
#> 3 3 -0.01595031 c  TRUE
#> 4 4 -0.82678895 d FALSE
#> 5 5 -1.51239965 e  TRUE
dtt1
#>        x           y      f      l
#>    <int>       <num> <char> <lgcl>
#> 1:     1 -0.87231588      a   TRUE
#> 2:     2  0.10668461      b  FALSE
#> 3:     3 -0.58701399      c   TRUE
#> 4:     4 -0.32785359      d  FALSE
#> 5:     5 -0.08536101      e   TRUE

# Use tribble() inside dtx() to easily create a data frame:
library(tibble)
dtx2 <- dtx(tribble(
  ~x, ~y, ~f,
   1,  3, 'a',
   2,  4, 'b'
))
dtx2
#>        x     y      f
#>    <num> <num> <char>
#> 1:     1     3      a
#> 2:     2     4      b
class(dtx2)
#> [1] "data.table" "data.frame"

# This is how you specify row names for dtf (data.frame)
dtf(x = 1:3, y = 4:6, .rownames = letters[1:3])
#>   x y
#> a 1 4
#> b 2 5
#> c 3 6