R重命名重复的col和行名(子索引) [英] R rename duplicate col and rownames (subindexing)

查看:159
本文介绍了R重命名重复的col和行名(子索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个善良的灵魂能够告诉我如何在R中做到这一点,我将非常感激:

I would very much appreciate if a kind soul could tell me how to do this in R:

给出具有重复的列和行的平方矩阵,例如

Given a squared matrix with duplicated columns and rows, such as

       1     1     2     2     2     2     3
1  0.000 0.000 0.048 0.048 0.048 0.048 0.059
1  0.000 0.000 0.048 0.048 0.048 0.048 0.059
2  0.048 0.048 0.000 0.000 0.000 0.000 0.059
2  0.048 0.048 0.000 0.000 0.000 0.000 0.059
2  0.048 0.048 0.000 0.000 0.000 0.000 0.059
2  0.048 0.048 0.000 0.000 0.000 0.000 0.059
3  0.059 0.059 0.059 0.059 0.059 0.059 0.000

在相同的列名和行名指定重复项的地方,我需要具有唯一的列名和行名,同时跟踪原始和重复的列名/行.也就是说,类似

where same col and row names designate duplicates, I require to have unique col and row names, while keeping track of original and duplicate cols/rows. That is, something like

        1    1a     2    2a    2b    2c     3
1   0.000 0.000 0.048 0.048 0.048 0.048 0.059
1a  0.000 0.000 0.048 0.048 0.048 0.048 0.059
2   0.048 0.048 0.000 0.000 0.000 0.000 0.059
2a  0.048 0.048 0.000 0.000 0.000 0.000 0.059
2b  0.048 0.048 0.000 0.000 0.000 0.000 0.059
2c  0.048 0.048 0.000 0.000 0.000 0.000 0.059
3   0.059 0.059 0.059 0.059 0.059 0.059 0.000

预先感谢

推荐答案

您可以使用?make.unique?make.names:

v <- as.character(c(1, 1, 2, 2, 2, 2, 3))
make.unique(v)
# [1] "1"   "1.1" "2"   "2.1" "2.2" "2.3" "3"

(您必须将其与rownamescolnames结合使用.)

(You have to combine this with rownames and colnames.)

这篇关于R重命名重复的col和行名(子索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆