如何仅命名 R 中 df 中的特定行?

How to name only particular rows in a df in R?

我最近试图只命名我的数据框中的部分行,但不知道该怎么做。我认为 df 的 'row.names' 可能会有所帮助,但看起来我无法命名某些行,我必须命名所有行才能使其工作。 至少这段代码没有改变任何行名:

example_df <- data.frame(rnorm(5), rnorm(5), rnorm(5))
row.names(example_df[c(1,2),]) <- c('11', '12')
row.names(example_df[3,]) <- 'a'

那么我怎样才能只更改部分行名呢?

这会起作用 -

example_df <- data.frame(rnorm(5), rnorm(5), rnorm(5))
row.names(example_df)[1:2] <- c('11', '12')
row.names(example_df)[3] <- 'a'

#    rnorm.5. rnorm.5..1  rnorm.5..2
# 11 -0.5374545 -1.0895643 -0.09938087
# 12 -0.6822140 -0.2806339  1.38078815
# a  -0.8664183 -0.5729183 -0.84851810
# 4  -0.9269735  0.4403557 -0.05622809
# 5   2.1156331 -1.1441339 -1.04363951