rownames()

rownames(x) <- value
Returns: character vector or NULL · Updated March 13, 2026 · Base Functions
vectors dimensions base

rownames() retrieves or sets the row names of matrices, data frames, and array-like objects. They are essential for working with tabular data and are the row equivalent of colnames() for columns.

Syntax

# Get row names
rownames(x)

# Set row names
rownames(x) <- value

Parameters

ParameterTypeDefaultDescription
xmatrix, data.frame, or arrayThe object whose row names to get or set
valuecharacter vector or NULLNew row names to assign

Examples

Basic usage

# Create a simple data frame
df <- data.frame(
  name = c("Alice", "Bob", "Carol"),
  age = c(25, 30, 35),
  score = c(85, 92, 78)
)

# Get row names (defaults to NULL for data frames)
rownames(df)
# [1] "1" "2" "3"

Setting row names

# Set row names
rownames(df) <- c("row_1", "row_2", "row_3")
df
#       name age score
# row_1 Alice  25     85
# row_2   Bob  30     92
# row_3 Carol  35     78

Working with matrices

# Create a matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)
rownames(mat) <- c("x", "y", "z")
mat
#   [,1] [,2] [,3]
# x    1    4    7
# y    2    5    8
# z    3    6    9

Common Patterns

Subset by row name:

df["row_1", ]
#   name age score
# 1 Alice  25     85

Check if row exists:

"row_1" %in% rownames(df)
# [1] TRUE

See Also