学习笔记,仅供参考,有错必纠
准备
实例数据集如下:
df <- data.frame(a = c("Ada", "Tim", "Black"),
b = c(1, 2, 3),
d = c(10, 15, 21))
> df
a b d
1 Ada 1 10
2 Tim 2 15
3 Black 3 21
rownames()
使用rownames()
函数替换:
rownames(df) <- df$a
df <- df[, -1]
> df
b d
Ada 1 10
Tim 2 15
Black 3 21
column_to_rownames()
使用column_to_rownames()
函数替换:
library(tibble)
df <- column_to_rownames(df, "a")
> df
b d
Ada 1 10
Tim 2 15
Black 3 21