row.names: Get and Set Row Names for Data Frames (original) (raw)

row.names R Documentation

Get and Set Row Names for Data Frames

Description

All data frames have row names, a character vector of length the number of rows with no duplicates nor missing values.

There are generic functions for getting and setting row names, with default methods for arrays. The description here is for the data.frame method.

`.rowNamesDF<-` is a (non-generic replacement) function to set row names for data frames, with extra argument make.names. This function only exists as workaround as we cannot easily change therow.names<- generic without breaking legacy code in existing packages.

Usage

row.names(x) row.names(x) <- value .rowNamesDF(x, make.names=FALSE) <- value

Arguments

x object of class "data.frame", or any other class for which a method has been defined.
make.names logical, i.e., one of FALSE, NA, TRUE, indicating what should happen if the specified row names, i.e.,value, are invalid, e.g., duplicated or NA. The default (is back compatible), FALSE, will signal an error, whereNA will “automatic” row names and TRUE will callmake.names(value, unique=TRUE) for constructing valid names.
value an object to be coerced to character unless an integer vector. It should have (after coercion) the same length as the number of rows of x with no duplicated nor missing values.NULL is also allowed: see ‘Details’.

Details

A data frame has (by definition) a vector of row names which has length the number of rows in the data frame, and contains neither missing nor duplicated values. Where a row names sequence has been added by the software to meet this requirement, they are regarded as ‘automatic’.

Row names are currently allowed to be integer or character, but for backwards compatibility (with R <= 2.4.0) row.names will always return a character vector. (Use attr(x, "row.names") if you need to retrieve an integer-valued set of row names.)

Using NULL for the value resets the row names toseq_len(nrow(x)), regarded as ‘automatic’.

Value

row.names returns a character vector.

row.names<- returns a data frame with the row names changed.

Note

row.names is similar to rownames for arrays, and it has a method that calls rownames for an array argument.

Row names of the form 1:n for n > 2 are stored internally in a compact form, which might be seen from C code or by deparsing but never via row.names orattr(x, "row.names"). Additionally, some names of this sort are marked as ‘automatic’ and handled differently byas.matrix and data.matrix (and potentially other functions). (All zero-row data frames are regarded as having automatic row names.)

References

Chambers, J. M. (1992)_Data for models._Chapter 3 of _Statistical Models in S_eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

See Also

data.frame, rownames, names.

.row_names_info for the internal representations.

Examples

To illustrate the note:

df <- data.frame(x = c(TRUE, FALSE, NA, NA), y = c(12, 34, 56, 78)) row.names(df) <- 1 : 4 attr(df, "row.names") #> 1:4 deparse(df) # or dput(df) ##--> c(NA, 4L) : Compact storage, not regarded as automatic.

row.names(df) <- NULL attr(df, "row.names") #> 1:4 deparse(df) # or dput(df) -- shows ##--> c(NA, -4L) : Compact storage, regarded as automatic.