简体   繁体   English

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

[英]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.

相关问题 R dplyr:如何使用组信息和编码列表中特定列的缺失条目向 df 添加额外的行? - R dplyr: How to add extra rows to a df using group info and missing entries of a particular column from a codelist? 如何在R中将一个df中的行与另一个df中的列进行匹配 - How to match rows from one df with columns in another df in R R函数如何取df的名称 - R function how to take the name of df R rbind 行到 df,结果按名称按组为多列计算 - R rbind rows to df with results calculated by group for multiple columns by name 如何基于特定值在df中插入空白行 - How to insert blank rows in a df based on a particular value R如何通过ro名称和列名称向df添加值 - R how to add a value to a df by name of ro and name of columns 当只有一行满足R中的条件时,如何删除属于特定组的所有行? - How to remove all rows belonging to a particular group when only one row fulfills the condition in R? 如何在R中将特定行转换为列 - How to transform particular rows into columns in R 使用 R 如何仅从目录中读取 csv 以某些字符开头并根据最后 4 个字母分配 df 名称的文件? - Using R how do I read csv from a directory only the files that begin with certain characters and assign a df name based on the last 4 letters? 如何识别R df中缺少的行-不使用NA - How to identify missing rows in a R df - without NA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM