[英]How to name only particular rows in a df in R?
I recently tried to name only part of rows in my dataframe, but don't know how to do this. 我最近尝试只命名数据框中的行的一部分,但不知道如何执行此操作。 I thought that maybe 'row.names' for df could help, but it looks like I can't name some rows, I must name all rows to make it work.
我以为df的“ row.names”可能会有所帮助,但看起来我无法命名某些行,我必须命名所有行才能使其正常工作。 At least this code didn't change any 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'
So how can I change only part of row names? 那么,如何仅更改行名称的一部分?
This will work - 这将起作用-
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.