Movatterモバイル変換


[0]ホーム

URL:


Title:Simple Data Frames
Version:3.3.0
Description:Provides a 'tbl_df' class (the 'tibble') with stricter checking and better formatting than the traditional data frame.
License:MIT + file LICENSE
URL:https://tibble.tidyverse.org/,https://github.com/tidyverse/tibble
BugReports:https://github.com/tidyverse/tibble/issues
Depends:R (≥ 3.4.0)
Imports:cli, lifecycle (≥ 1.0.0), magrittr, methods, pillar (≥1.8.1), pkgconfig, rlang (≥ 1.0.2), utils, vctrs (≥ 0.5.0)
Suggests:bench, bit64, blob, brio, callr, DiagrammeR, dplyr, evaluate,formattable, ggplot2, here, hms, htmltools, knitr, lubridate,nycflights13, pkgload, purrr, rmarkdown, stringi, testthat (≥3.0.2), tidyr, withr
VignetteBuilder:knitr
Encoding:UTF-8
RoxygenNote:7.3.2.9000
Config/testthat/edition:3
Config/testthat/parallel:true
Config/testthat/start-first:vignette-formats, as_tibble, add,invariants
Config/autostyle/scope:line_breaks
Config/autostyle/strict:true
Config/autostyle/rmd:false
Config/Needs/website:tidyverse/tidytemplate
NeedsCompilation:yes
Packaged:2025-06-07 08:54:33 UTC; kirill
Author:Kirill MüllerORCID iD [aut, cre], Hadley Wickham [aut], Romain Francois [ctb], Jennifer Bryan [ctb], RStudio [cph, fnd]
Maintainer:Kirill Müller <kirill@cynkra.com>
Repository:CRAN
Date/Publication:2025-06-08 12:10:02 UTC

tibble: Simple Data Frames

Description

logo

Provides a 'tbl_df' class (the 'tibble') with stricter checking and better formatting than the traditional data frame.

Details

[Stable]

The tibble package provides utilities for handlingtibbles, where"tibble" is a colloquial term for the S3tbl_df class. Thetbl_dfclass is a special case of the basedata.frameclass, developed in response to lessons learned over many years of dataanalysis with data frames.

Tibble is the central data structure for the set of packages known as thetidyverse, includingdplyr,ggplot2,tidyr, andreadr.

General resources:

Resources on specific topics:

Author(s)

Maintainer: Kirill Müllerkirill@cynkra.com (ORCID)

Authors:

Other contributors:

See Also

Useful links:


Add columns to a data frame

Description

This is a convenient way to add one or more columns to an existing dataframe.

Usage

add_column(  .data,  ...,  .before = NULL,  .after = NULL,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"))

Arguments

.data

Data frame to append to.

...

<dynamic-dots>Name-value pairs, passed on totibble(). All values must havethe same size of.data or size 1.

.before,.after

One-based column index or column name where to add thenew columns, default: after last column.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they areunique,

  • "universal": Make the namesunique and syntactic

  • "unique_quiet": Same as"unique", but "quiet"

  • "universal_quiet": Same as"universal", but "quiet"

  • a function: apply custom name repair (e.g.,.name_repair = make.namesfor names in the style of base R).

  • A purrr-style anonymous function, seerlang::as_function()

This argument is passed on asrepair tovctrs::vec_as_names().See there for more details on these terms and the strategies usedto enforce them.

See Also

Other addition:add_row()

Examples

# add_column ---------------------------------df <- tibble(x = 1:3, y = 3:1)df %>% add_column(z = -1:1, w = 0)df %>% add_column(z = -1:1, .before = "y")# You can't overwrite existing columnstry(df %>% add_column(x = 4:6))# You can't create new observationstry(df %>% add_column(z = 1:5))

Add rows to a data frame

Description

This is a convenient way to add one or more rows of data to an existing dataframe. Seetribble() for an easy way to create an completedata frame row-by-row. Usetibble_row() to ensure that the new datahas only one row.

add_case() is an alias ofadd_row().

Usage

add_row(.data, ..., .before = NULL, .after = NULL)

Arguments

.data

Data frame to append to.

...

<dynamic-dots>Name-value pairs, passed on totibble(). Values can be definedonly for columns that already exist in.data and unset columns will get anNA value.

.before,.after

One-based row index where to add the new rows,default: after last row.

See Also

Other addition:add_column()

Examples

# add_row ---------------------------------df <- tibble(x = 1:3, y = 3:1)df %>% add_row(x = 4, y = 0)# You can specify where to add the new rowsdf %>% add_row(x = 4, y = 0, .before = 2)# You can supply vectors, to add multiple rows (this isn't# recommended because it's a bit hard to read)df %>% add_row(x = 4:5, y = 0:-1)# Use tibble_row() to add one row onlydf %>% add_row(tibble_row(x = 4, y = 0))try(df %>% add_row(tibble_row(x = 4:5, y = 0:-1)))# Absent variables get missing valuesdf %>% add_row(x = 4)# You can't create new variablestry(df %>% add_row(z = 10))

Coerce lists, matrices, and more to data frames

Description

as_tibble() turns an existing object, such as a data frame ormatrix, into a so-called tibble, a data frame with classtbl_df. This isin contrast withtibble(), which builds a tibble from individual columns.as_tibble() is totibble() asbase::as.data.frame() is tobase::data.frame().

as_tibble() is an S3 generic, with methods for:

as_tibble_row() converts a vector to a tibble with one row.If the input is a list, all elements must have size one.

as_tibble_col() converts a vector to a tibble with one column.

Usage

as_tibble(  x,  ...,  .rows = NULL,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"),  rownames = pkgconfig::get_config("tibble::rownames", NULL))## S3 method for class 'data.frame'as_tibble(  x,  validate = NULL,  ...,  .rows = NULL,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"),  rownames = pkgconfig::get_config("tibble::rownames", NULL))## S3 method for class 'list'as_tibble(  x,  validate = NULL,  ...,  .rows = NULL,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"))## S3 method for class 'matrix'as_tibble(x, ..., validate = NULL, .name_repair = NULL)## S3 method for class 'table'as_tibble(x, `_n` = "n", ..., n = `_n`, .name_repair = "check_unique")## S3 method for class ''NULL''as_tibble(x, ...)## Default S3 method:as_tibble(x, ...)as_tibble_row(  x,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"))as_tibble_col(x, column_name = "value")

Arguments

x

A data frame, list, matrix, or other object that could reasonably becoerced to a tibble.

...

Unused, for extensibility.

.rows

The number of rows, useful to create a 0-column tibble orjust as an additional check.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they areunique,

  • "universal": Make the namesunique and syntactic

  • "unique_quiet": Same as"unique", but "quiet"

  • "universal_quiet": Same as"universal", but "quiet"

  • a function: apply custom name repair (e.g.,.name_repair = make.namesfor names in the style of base R).

  • A purrr-style anonymous function, seerlang::as_function()

This argument is passed on asrepair tovctrs::vec_as_names().See there for more details on these terms and the strategies usedto enforce them.

rownames

How to treat existing row names of a data frame or matrix:

  • NULL: remove row names. This is the default.

  • NA: keep row names.

  • A string: the name of a new column. Existing rownames are transferredinto this column and therow.names attribute is deleted.No name repair is applied to the new column name, even ifx already containsa column of that name.Useas_tibble(rownames_to_column(...)) to safeguard against this case.

Read more inrownames.

_n,validate

[Soft-deprecated]

For compatibility only, do not use for new code.

n

Name for count column, default:"n".

column_name

Name of the column.

Row names

The default behavior is to silently remove row names.

New code should explicitly convert row names to a new column using therownames argument.

For existing code that relies on the retention of row names, callpkgconfig::set_config("tibble::rownames" = NA) in your script or in yourpackage's.onLoad() function.

Life cycle

Usingas_tibble() for vectors is superseded as of version 3.0.0,prefer the more expressiveas_tibble_row() andas_tibble_col() variants for new code.

See Also

tibble() constructs a tibble from individual columns.enframe()converts a named vector to a tibble with a column of names and column ofvalues. Name repair is implemented usingvctrs::vec_as_names().

Examples

m <- matrix(rnorm(50), ncol = 5)colnames(m) <- c("a", "b", "c", "d", "e")df <- as_tibble(m)as_tibble_row(c(a = 1, b = 2))as_tibble_row(list(c = "three", d = list(4:5)))as_tibble_row(1:3, .name_repair = "unique")as_tibble_col(1:3)as_tibble_col(  list(c = "three", d = list(4:5)),  column_name = "data")

Format a character vector

Description

[Experimental]

Constructs a character vector that can be formatted with predefined minimum widthor without width restrictions, and where the abbreviation style can be configured.

The formatting is applied when the vector is printed or formatted,and also in a tibble column.

set_char_opts() adds formatting options to an arbitrary character vector,useful for composing with other types.

Usage

char(  x,  ...,  min_chars = NULL,  shorten = c("back", "front", "mid", "abbreviate"))set_char_opts(  x,  ...,  min_chars = NULL,  shorten = c("back", "front", "mid", "abbreviate"))

Arguments

x

A character vector.

...

These dots are for future extensions and must be empty.

min_chars

The minimum width to allocate to this column, defaults to 15.The"pillar.min_chars"option is not consulted.

shorten

How to abbreviate the data if necessary:

  • "back" (default): add an ellipsis at the end

  • "front": add an ellipsis at the front

  • "mid": add an ellipsis in the middle

  • "abbreviate": useabbreviate()

See Also

Other vector classes:num()

Examples

# Display as a vector:char(letters[1:3])# Space constraints:rand_strings <- stringi::stri_rand_strings(10, seq(40, 22, by = -2))# Plain character vectors get truncated if space is limited:data_with_id <- function(id) {  tibble(    id,    some_number_1 = 1, some_number_2 = 2, some_number_3 = 3,    some_number_4 = 4, some_number_5 = 5, some_number_6 = 6,    some_number_7 = 7, some_number_8 = 8, some_number_9 = 9  )}data_with_id(rand_strings)# Use char() to avoid or control truncationdata_with_id(char(rand_strings, min_chars = 24))data_with_id(char(rand_strings, min_chars = Inf))data_with_id(char(rand_strings, min_chars = 24, shorten = "mid"))# Lorem Ipsum, one sentence per row.lipsum <- unlist(strsplit(stringi::stri_rand_lipsum(1), "(?<=[.]) +", perl = TRUE))tibble(  back = char(lipsum, shorten = "back"),  front = char(lipsum, shorten = "front"),  mid = char(lipsum, shorten = "mid"))tibble(abbr = char(lipsum, shorten = "abbreviate"))

Deprecated functions

Description

[Deprecated]

Usetibble() instead ofdata_frame().

Userlang::quasiquotation instead oftibble_(),data_frame_(), andlst_().

Useas_tibble() instead ofas_data_frame() oras.tibble(), but mind thenew signature and semantics.

Usetribble() instead offrame_data().

Usage

data_frame(...)tibble_(xs)data_frame_(xs)lst_(xs)as_data_frame(x, ...)as.tibble(x, ...)frame_data(...)

Converting vectors to data frames, and vice versa

Description

enframe() converts named atomic vectors or lists to one- or two-columndata frames.For a list, the result will be a nested tibble with a column of typelist.For unnamed vectors, the natural sequence is used as name column.

deframe() converts two-column data frames to a named vector or list,using the first column as name and the second column as value.If the input has only one column, an unnamed vector is returned.

Usage

enframe(x, name = "name", value = "value")deframe(x)

Arguments

x

A vector (forenframe()) or a data frame with one or two columns(fordeframe()).

name,value

Names of the columns that store the names and values.Ifname isNULL, a one-column tibble is returned;value cannot beNULL.

Value

Forenframe(), atibble with two columns (ifname is notNULL, the default)or one column (otherwise).

Fordeframe(), a vector (named or unnamed).

Examples

enframe(1:3)enframe(c(a = 5, b = 7))enframe(list(one = 1, two = 2:3, three = 4:6))deframe(enframe(3:1))deframe(tibble(a = 1:3))deframe(tibble(a = as.list(1:3)))

Printing tibbles

Description

One of the main features of thetbl_df class is the printing:

Printing can be tweaked for a one-off call by callingprint() explicitlyand setting arguments liken andwidth. More persistent control isavailable by setting the options described inpillar::pillar_options.See alsovignette("digits") for a comparison to base options,andvignette("numbers") that showcasesnum() andchar()for creating columns with custom formatting options.

As of tibble 3.1.0, printing is handled entirely by thepillar package.If you implement a package that extends tibble,the printed output can be customized in various ways.Seevignette("extending", package = "pillar") for details,andpillar::pillar_options for options that control the display in the console.

Usage

## S3 method for class 'tbl_df'print(  x,  width = NULL,  ...,  n = NULL,  max_extra_cols = NULL,  max_footer_lines = NULL)## S3 method for class 'tbl_df'format(  x,  width = NULL,  ...,  n = NULL,  max_extra_cols = NULL,  max_footer_lines = NULL)

Arguments

x

Object to format or print.

width

Width of text output to generate. This defaults toNULL, whichmeans use thewidthoption.

...

These dots are for future extensions and must be empty.

n

Number of rows to show. IfNULL, the default, will print all rowsif less than theprint_maxoption.Otherwise, will print as many rows as specified by theprint_minoption.

max_extra_cols

Number of extra columns to print abbreviated information for,if the width is too small for the entire tibble. IfNULL,themax_extra_colsoption is used.The previously definedn_extra argument is soft-deprecated.

max_footer_lines

Maximum number of footer lines. IfNULL,themax_footer_linesoption is used.

Examples

print(as_tibble(mtcars))print(as_tibble(mtcars), n = 1)print(as_tibble(mtcars), n = 3)print(as_tibble(trees), n = 100)print(mtcars, width = 10)mtcars2 <- as_tibble(cbind(mtcars, mtcars), .name_repair = "unique")print(mtcars2, n = 25, max_extra_cols = 3)print(nycflights13::flights, max_footer_lines = 1)print(nycflights13::flights, width = Inf)

Row-wise matrix creation

Description

Create matrices laying out the data in rows, similar tomatrix(..., byrow = TRUE), with a nicer-to-read syntax.This is useful for small matrices, e.g. covariance matrices, where readabilityis important. The syntax is inspired bytribble().

Usage

frame_matrix(...)

Arguments

...

<dynamic-dots>Arguments specifying the structure of aframe_matrix.Column names should be formulas, and may only appear before the data.These arguments are processed withrlang::list2()and support unquote viarlang::!! and unquote-splice viarlang::!!!.

Value

Amatrix.

See Also

Seerlang::quasiquotation for more details on tidy dots semantics,i.e. exactly how the... argument is processed.

Examples

frame_matrix(  ~col1, ~col2,  1,     3,  5,     2)

Deprecated test for tibble-ness

Description

[Soft-deprecated]

Please useis_tibble() instead.

Usage

is.tibble(x)

Arguments

x

An object


Test if the object is a tibble

Description

This function returnsTRUE for tibbles or subclasses thereof,andFALSE for all other objects, including regular data frames.

Usage

is_tibble(x)

Arguments

x

An object

Value

TRUE if the object inherits from thetbl_df class.


Build a list

Description

lst() constructs a list, similar tobase::list(), but with some of thesame features astibble().lst() builds components sequentially. Whendefining a component, you can refer to components created earlier in thecall.lst() also generates missing names automatically.

Seerlang::list2() for a simpler and faster alternative without tibble'sevaluation and auto-name semantics.

Usage

lst(...)

Arguments

...

<dynamic-dots>A set of name-value pairs. These arguments areprocessed withrlang::quos() and support unquote viarlang::!! andunquote-splice viarlang::!!!. Use⁠:=⁠ to create columns that start with a dot.

Arguments are evaluated sequentially.You can refer to previously created elements directly or using therlang::.datapronoun.To refer explicitly to objects in the calling environment, userlang::!! orrlang::.env, e.g.!!.data or.env$.data for the special case of an objectnamed.data.

Value

A named list.

Examples

# the value of n can be used immediately in the definition of xlst(n = 5, x = runif(n))# missing names are constructed from user's inputlst(1:3, z = letters[4:6], runif(3))a <- 1:3b <- letters[4:6]lst(a, b)# pre-formed quoted expressions can be used with lst() and then# unquoted (with !!) or unquoted and spliced (with !!!)n1 <- 2n2 <- 3n_stuff <- quote(n1 + n2)x_stuff <- quote(seq_len(n))lst(!!!list(n = n_stuff, x = x_stuff))lst(n = !!n_stuff, x = !!x_stuff)lst(n = 4, x = !!x_stuff)lst(!!!list(n = 2, x = x_stuff))

Name repair

Description

Please reviewvctrs::vec_as_names().


Superseded functions for name repair

Description

[Superseded]

tidy_names(),set_tidy_names(), andrepair_names() were early effortsto facilitatepost hoc name repair in tibble, given thattibble() andas_tibble() did not do this.

From tibble v2.0.0, the.name_repair argument gives direct access to threespecific levels of name repair:minimal,unique, anduniversal.Seevctrs::vec_as_names() for the implementation of the underlying logic.

Usage

tidy_names(name, syntactic = FALSE, quiet = FALSE)set_tidy_names(x, syntactic = FALSE, quiet = FALSE)repair_names(x, prefix = "V", sep = "")

Arguments

name

Anames attribute, usually a character vector.

syntactic

Should names be made syntactically valid? IfFALSE, usessame logic as.name_repair = "unique". IfTRUE, uses same logic as.name_repair = "universal".

quiet

Whether to suppress messages about name repair.

x

A vector.

prefix

A string, the prefix to use for new column names.

sep

A string inserted between the column name and de-duplicatingnumber.

Value

x with repaired names or a repaired version ofname.

Life cycle

These functions are superseded. Therepair_names() logicwill also remain available invctrs::vec_as_names_legacy().

tibble(..., `.name_repair = "unique"`)## is preferred todf <- tibble(...)set_tidy_names(df, syntactic = FALSE)tibble(..., `.name_repair = "universal"`)## is preferred todf <- tibble(...)set_tidy_names(df, syntactic = TRUE)

Tibble constructor and validator

Description

Creates or validates a subclass of a tibble.These function is mostly useful for package authors that implement subclassesof a tibble, likesf ortsibble.

new_tibble() creates a new object as a subclass oftbl_df,tbl anddata.frame.This function is optimized for performance, checks are reduced to a minimum.Seevctrs::new_data_frame() for details.

validate_tibble() checks a tibble for internal consistency.Correct behavior can be guaranteed only if this functionruns without raising an error.

Usage

new_tibble(x, ..., nrow = NULL, class = NULL, subclass = NULL)validate_tibble(x)

Arguments

x

A tibble-like object.

...

Name-value pairs of additional attributes.

nrow

The number of rows, inferred fromx if omitted.

class

Subclasses to assign to the new object, default: none.

subclass

Deprecated, retained for compatibility. Please use theclass argument.

Construction

Fornew_tibble(),x must be a list.Thenrow argument may be omitted as of tibble 3.1.4.If present, every element of the listx should havevctrs::vec_size()equal to this value.(But this is not checked by the constructor).This takes the place of the "row.names" attribute in a data frame.x must have names (or be empty),but the names are not checked for correctness.

Validation

validate_tibble() checks for "minimal" namesand that all columns are vectors, data frames or matrices.It also makes sure that all columns have the same length,and thatvctrs::vec_size() is consistent with the data.

See Also

tibble() andas_tibble() for ways to construct a tibblewith recycling of scalars and automatic name repair,andvctrs::df_list() andvctrs::new_data_frame()for lower-level implementations.

Examples

# The nrow argument can be omitted:new_tibble(list(a = 1:3, b = 4:6))# Existing row.names attributes are ignored:try(validate_tibble(new_tibble(trees, nrow = 3)))# The length of all columns must be compatible with the nrow argument:try(validate_tibble(new_tibble(list(a = 1:3, b = 4:6), nrow = 2)))

Format a numeric vector

Description

[Experimental]

Constructs a numeric vector that can be formatted with predefinedsignificant digits, or with a maximum or fixed number of digitsafter the decimal point.Scaling is supported, as well as forcing a decimal, scientificor engineering notation.If a label is given, it is shown in the header of a column.

The formatting is applied when the vector is printed or formatted,and also in a tibble column.The formatting annotation and the class survives most arithmetic transformations,the most notable exceptions arevar() andsd().

set_num_opts() adds formatting options to an arbitrary numeric vector,useful for composing with other types.

Usage

num(  x,  ...,  sigfig = NULL,  digits = NULL,  label = NULL,  scale = NULL,  notation = c("fit", "dec", "sci", "eng", "si"),  fixed_exponent = NULL,  extra_sigfig = NULL)set_num_opts(  x,  ...,  sigfig = NULL,  digits = NULL,  label = NULL,  scale = NULL,  notation = c("fit", "dec", "sci", "eng", "si"),  fixed_exponent = NULL,  extra_sigfig = NULL)

Arguments

x

A numeric vector.

...

These dots are for future extensions and must be empty.

sigfig

Define the number of significant digits to show. Must be one or greater.The"pillar.sigfig"option is not consulted.Can't be combined withdigits.

digits

Number of digits after the decimal points to show.Positive numbers specify the exact number of digits to show.Negative numbers specify (after negation) the maximum number of digits to show.Withdigits = 2, the numbers 1.2 and 1.234 are printed as 1.20 and 1.23,withdigits = -2 as 1.2 and 1.23, respectively.Can't be combined withsigfig.

label

A label to show instead of the type description.

scale

Multiplier to apply to the data before showing.Useful for displaying e.g. percentages.Must be combined withlabel.

notation

One of"fit","dec","sci","eng", or"si".

  • "fit": Use decimal notation if it fits and if it consumes 13 digits or less,otherwise use scientific notation. (The default for numeric pillars.)

  • "dec": Use decimal notation, regardless of width.

  • "sci": Use scientific notation.

  • "eng": Use engineering notation, i.e. scientific notationusing exponents that are a multiple of three.

  • "si": Use SI notation, prefixes between1e-24 and1e24 are supported.

fixed_exponent

Use the same exponent for all numbers in scientific, engineering or SI notation.-Inf uses the smallest,+Inf the largest fixed_exponent present in the data.The default is to use varying exponents.

extra_sigfig

IfTRUE, increase the number of significant digits if the data consists ofnumbers of the same magnitude with subtle differences.

See Also

Other vector classes:char()

Examples

# Display as a vectornum(9:11 * 100 + 0.5)# Significant figurestibble(  x3 = num(9:11 * 100 + 0.5, sigfig = 3),  x4 = num(9:11 * 100 + 0.5, sigfig = 4),  x5 = num(9:11 * 100 + 0.5, sigfig = 5),)# Maximum digits after the decimal pointstibble(  x0 = num(9:11 * 100 + 0.5, digits = 0),  x1 = num(9:11 * 100 + 0.5, digits = -1),  x2 = num(9:11 * 100 + 0.5, digits = -2),)# Use fixed digits and a currency labeltibble(  usd = num(9:11 * 100 + 0.5, digits = 2, label = "USD"),  gbp = num(9:11 * 100 + 0.5, digits = 2, label = "£"),  chf = num(9:11 * 100 + 0.5, digits = 2, label = "SFr"))# Scaletibble(  small  = num(9:11 / 1000 + 0.00005, label = "%", scale = 100),  medium = num(9:11 / 100 + 0.0005, label = "%", scale = 100),  large  = num(9:11 / 10 + 0.005, label = "%", scale = 100))# Notationtibble(  sci = num(10^(-13:6), notation = "sci"),  eng = num(10^(-13:6), notation = "eng"),  si  = num(10^(-13:6), notation = "si"),  dec = num(10^(-13:6), notation = "dec"))# Fixed exponenttibble(  scimin = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -Inf),  engmin = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = -Inf),  simin  = num(10^(-7:6) * 123, notation = "si", fixed_exponent = -Inf))tibble(  scismall = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -3),  scilarge = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = 3),  scimax   = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = Inf))#' Extra significant digitstibble(  default = num(100 + 1:3 * 0.001),  extra1 = num(100 + 1:3 * 0.001, extra_sigfig = TRUE),  extra2 = num(100 + 1:3 * 0.0001, extra_sigfig = TRUE),  extra3 = num(10000 + 1:3 * 0.00001, extra_sigfig = TRUE))

Objects exported from other packages

Description

These objects are imported from other packages. Follow the linksbelow to see their documentation.

magrittr

%>%

pillar

glimpse,obj_sum,size_sum,tbl_sum,type_sum

rlang

has_name


Tools for working with row names

Description

While a tibble can have row names (e.g., when converting from a regular dataframe), they are removed when subsetting with the[ operator.A warning will be raised when attempting to assign non-NULL row namesto a tibble.Generally, it is best to avoid row names, because they are basically acharacter column with different semantics than every other column.

These functions allow to you detect if a data frame has row names(has_rownames()), remove them (remove_rownames()), or convertthem back-and-forth between an explicit column (rownames_to_column()andcolumn_to_rownames()).Also included isrowid_to_column(), which adds a column at the start of thedataframe of ascending sequential row ids starting at 1. Note that this willremove any existing row names.

Usage

has_rownames(.data)remove_rownames(.data)rownames_to_column(.data, var = "rowname")rowid_to_column(.data, var = "rowid")column_to_rownames(.data, var = "rowname")

Arguments

.data

A data frame.

var

Name of column to use for rownames.

Value

column_to_rownames() always returns a data frame.has_rownames() returns a scalar logical.All other functions return an object of the same class as the input.

Examples

# Detect row names ----------------------------------------------------has_rownames(mtcars)has_rownames(trees)# Remove row names ----------------------------------------------------remove_rownames(mtcars) %>% has_rownames()# Convert between row names and column --------------------------------mtcars_tbl <- rownames_to_column(mtcars, var = "car") %>% as_tibble()mtcars_tblcolumn_to_rownames(mtcars_tbl, var = "car") %>% head()# Adding rowid as a column --------------------------------------------rowid_to_column(trees) %>% head()

Subsetting tibbles

Description

Accessing columns, rows, or cells via$,[[, or[ is mostly similar toregular data frames. However, thebehavior is different for tibbles and data frames in some cases:

Unstable return type and implicit partial matching can lead to surprises andbugs that are hard to catch. If you rely on code that requires the originaldata frame behavior, coerce to a data frame viaas.data.frame().

Usage

## S3 method for class 'tbl_df'x$name## S3 method for class 'tbl_df'x[[i, j, ..., exact = TRUE]]## S3 method for class 'tbl_df'x[i, j, drop = FALSE, ...]## S3 replacement method for class 'tbl_df'x$name <- value## S3 replacement method for class 'tbl_df'x[[i, j, ...]] <- value## S3 replacement method for class 'tbl_df'x[i, j, ...] <- value

Arguments

x

A tibble.

name

Aname or a string.

i,j

Row and column indices. Ifj is omitted,i is used as column index.

...

Ignored.

exact

Ignored, with a warning.

drop

Coerce to a vector if fetching one column viatbl[, j] .DefaultFALSE, ignored when accessing a column viatbl[j] .

value

A value to store in a row, column, range or cell.Tibbles are stricter than data frames in what is accepted here.

Details

For better compatibility with older code written for regular data frames,[ supports adrop argument which defaults toFALSE.New code should use[[ to turn a column into a vector.

Examples

df <- data.frame(a = 1:3, bc = 4:6)tbl <- tibble(a = 1:3, bc = 4:6)# Subsetting single columns:df[, "a"]tbl[, "a"]tbl[, "a", drop = TRUE]as.data.frame(tbl)[, "a"]# Subsetting single rows with the drop argument:df[1, , drop = TRUE]tbl[1, , drop = TRUE]as.list(tbl[1, ])# Accessing non-existent columns:df$btbl$bdf[["b", exact = FALSE]]tbl[["b", exact = FALSE]]df$bd <- c("n", "e", "w")tbl$bd <- c("n", "e", "w")df$btbl$bdf$b <- 7:9tbl$b <- 7:9df$btbl$b# Identical behavior:tbl[1, ]tbl[1, c("bc", "a")]tbl[, c("bc", "a")]tbl[c("bc", "a")]tbl["a"]tbl$atbl[["a"]]

tbl_df class

Description

Thetbl_df class is a subclass ofdata.frame,created in order to have different default behaviour. The colloquial term"tibble" refers to a data frame that has thetbl_df class. Tibble is thecentral data structure for the set of packages known as thetidyverse, includingdplyr,ggplot2,tidyr, andreadr.

The general ethos is that tibbles are lazy and surly: they do less andcomplain more than basedata.frames. This forcesproblems to be tackled earlier and more explicitly, typically leading to codethat is more expressive and robust.

Properties oftbl_df

Objects of classtbl_df have:

Behavior oftbl_df

How default behaviour of tibbles differs from that ofdata.frames, during creation and access:

Seevignette("invariants") for a detailed description of the behavior.

Furthermore, printing and inspection are a very high priority.The goal is to convey as much information as possible, in a concise way,even for large and complex tibbles. Read more informatting.

See Also

tibble(),as_tibble(),tribble(),print.tbl(),glimpse()


Build a data frame

Description

tibble() constructs a data frame. It is used likebase::data.frame(), butwith a couple notable differences:

tibble_row() constructs a data frame that is guaranteed to occupy one row.Vector columns are required to have size one, non-vector columns are wrappedin a list.

Usage

tibble(  ...,  .rows = NULL,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"))tibble_row(  ...,  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",    "universal_quiet"))

Arguments

...

<dynamic-dots>A set of name-value pairs. These arguments areprocessed withrlang::quos() and support unquote viarlang::!! andunquote-splice viarlang::!!!. Use⁠:=⁠ to create columns that start with a dot.

Arguments are evaluated sequentially.You can refer to previously created elements directly or using therlang::.datapronoun.To refer explicitly to objects in the calling environment, userlang::!! orrlang::.env, e.g.!!.data or.env$.data for the special case of an objectnamed.data.

.rows

The number of rows, useful to create a 0-column tibble orjust as an additional check.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they areunique,

  • "universal": Make the namesunique and syntactic

  • "unique_quiet": Same as"unique", but "quiet"

  • "universal_quiet": Same as"universal", but "quiet"

  • a function: apply custom name repair (e.g.,.name_repair = make.namesfor names in the style of base R).

  • A purrr-style anonymous function, seerlang::as_function()

This argument is passed on asrepair tovctrs::vec_as_names().See there for more details on these terms and the strategies usedto enforce them.

Value

A tibble, which is a colloquial term for an object of classtbl_df. Atbl_df object is also a dataframe, i.e. it has classdata.frame.

See Also

Useas_tibble() to turn an existing object into a tibble. Useenframe() to convert a named vector into a tibble. Name repair isdetailed invctrs::vec_as_names().Seerlang::quasiquotation for more details on tidy dots semantics,i.e. exactly how the... argument is processed.

Examples

# Unnamed arguments are named with their expression:a <- 1:5tibble(a, a * 2)# Scalars (vectors of length one) are recycled:tibble(a, b = a * 2, c = 1)# Columns are available in subsequent expressions:tibble(x = runif(10), y = x * 2)# tibble() never coerces its inputs,str(tibble(letters))str(tibble(x = list(diag(1), diag(2))))# or munges column names (unless requested),tibble(`a + b` = 1:5)# but it forces you to take charge of names, if they need repair:try(tibble(x = 1, x = 2))tibble(x = 1, x = 2, .name_repair = "unique")tibble(x = 1, x = 2, .name_repair = "minimal")## By default, non-syntactic names are allowed,df <- tibble(`a 1` = 1, `a 2` = 2)## because you can still index by name:df[["a 1"]]df$`a 1`with(df, `a 1`)## Syntactic names are easier to work with, though, and you can request them:df <- tibble(`a 1` = 1, `a 2` = 2, .name_repair = "universal")df$a.1## You can specify your own name repair function:tibble(x = 1, x = 2, .name_repair = make.unique)fix_names <- function(x) gsub("\\s+", "_", x)tibble(`year 1` = 1, `year 2` = 2, .name_repair = fix_names)## purrr-style anonymous functions and constants## are also supportedtibble(x = 1, x = 2, .name_repair = ~ make.names(., unique = TRUE))tibble(x = 1, x = 2, .name_repair = ~ c("a", "b"))# Tibbles can contain columns that are tibbles or matrices# if the number of rows is compatible. Unnamed tibbled are# spliced, i.e. the inner columns are inserted into the# tibble under construction.tibble(  a = 1:3,  tibble(    b = 4:6,    c = 7:9  ),  d = tibble(    e = tibble(      f = b    )  ))tibble(  a = 1:3,  b = diag(3),  c = cor(trees),  d = Titanic[1:3, , , ])# Data can not contain tibbles or matrices with incompatible number of rows:try(tibble(a = 1:3, b = tibble(c = 4:7)))# Use := to create columns with names that start with a dot:tibble(.dotted := 3)# This also works, but might break in the future:tibble(.dotted = 3)# You can unquote an expression:x <- 3tibble(x = 1, y = x)tibble(x = 1, y = !!x)# You can splice-unquote a list of quosures and expressions:tibble(!!!list(x = rlang::quo(1:10), y = quote(x * 2)))# Use .data, .env and !! to refer explicitly to columns or outside objectsa <- 1tibble(a = 2, b = a)tibble(a = 2, b = .data$a)tibble(a = 2, b = .env$a)tibble(a = 2, b = !!a)try(tibble(a = 2, b = .env$bogus))try(tibble(a = 2, b = !!bogus))# Use tibble_row() to construct a one-row tibble:tibble_row(a = 1, lm = lm(Height ~ Girth + Volume, data = trees))

Package options

Description

Options that affect interactive display.Seepillar::pillar_options for options that affect display on the console,andcli::num_ansi_colors() for enabling and disabling colored outputvia ANSI sequences like⁠[3m[38;5;246m[39m[23m⁠.

Usage

tibble_options

Details

These options can be set viaoptions() and queried viagetOption().For this, add atibble. prefix (the package name and a dot) to the option name.Example: for an optionfoo, useoptions(tibble.foo = value) to set itandgetOption("tibble.foo") to retrieve the current value.An option value ofNULL means that the default is used.

Options for the tibble package

Examples

# Default setting:getOption("tibble.view_max")# Change for the duration of the session:old <- options(tibble.view_max = 100)# view() would show only 100 rows e.g. for a lazy data frame# Change back to the original value:options(old)# Local scope:local({  rlang::local_options(tibble.view_max = 100)  # view() would show only 100 rows e.g. for a lazy data frame})# view() would show the default 1000 rows e.g. for a lazy data frame

Row-wise tibble creation

Description

Createtibbles using an easier to read row-by-row layout.This is useful for small tables of data where readability isimportant. Please seetibble-package for a general introduction.

Usage

tribble(...)

Arguments

...

<dynamic-dots>Arguments specifying the structure of atibble.Variable names should be formulas, and may only appear before the data.These arguments are processed withrlang::list2()and support unquote viarlang::!! and unquote-splice viarlang::!!!.

Value

Atibble.

See Also

Seerlang::quasiquotation for more details on tidy dots semantics,i.e. exactly how the... argument is processed.

Examples

tribble(  ~colA, ~colB,  "a",   1,  "b",   2,  "c",   3)# tribble will create a list column if the value in any cell is# not a scalartribble(  ~x,  ~y,  "a", 1:3,  "b", 4:6)# Use dplyr::mutate(dplyr::across(...)) to assign an explicit typetribble(  ~a, ~b, ~c,  1, "2000-01-01", "1.5") %>%  dplyr::mutate(    dplyr::across(a, as.integer),    dplyr::across(b, as.Date)  )

Legacy printing

Description

[Deprecated]As of tibble 3.1.0, printing is handled entirely by thepillar package.Do not use this function.If you implement a package that extend tibble,the printed output can be customized in various ways.Seevignette("extending", package = "pillar") for details.

Usage

trunc_mat(x, n = NULL, width = NULL, n_extra = NULL)

Arguments

x

Object to format or print.

n

Number of rows to show. IfNULL, the default, will print all rowsif less than optiontibble.print_max. Otherwise, will printtibble.print_min rows.

width

Width of text output to generate. This defaults toNULL, whichmeans usegetOption("tibble.width") or (if alsoNULL)getOption("width"); the latter displays only the columns that fit on onescreen. You can also setoptions(tibble.width = Inf) to override thisdefault and always print all columns, this may be slow for very wide tibbles.

n_extra

Number of extra columns to print abbreviated information for,if the width is too small for the entire tibble. IfNULL, the default,will print information about at mosttibble.max_extra_cols extra columns.

Value

An object with aprint() method that will print the inputsimilarly to a tibble.The internal data format is an implementation detail, do not rely on it.


View an object

Description

[Experimental]

Callsutils::View() on the input and returns it, invisibly.If the input is not a data frame, it is processed using a variant ofas.data.frame(head(x, n)).A message is printed if the number of rows exceedsn.This function has no effect in noninteractive sessions.

Usage

view(x, title = NULL, ..., n = NULL)

Arguments

x

The object to display.

title

The title to use for the display, by defaultthe deparsed expression is used.

...

Unused, must be empty.

n

Maximum number of rows to display. Only used ifx is not adata frame. Uses theview_maxoption by default.

Details

The RStudio IDE overridesutils::View(), this is picked upcorrectly.


[8]ページ先頭

©2009-2025 Movatter.jp