使非唯一行名称唯一

时间:2018-03-20 10:23:46

标签: r

我在R中有一个data.frame对象,我想要有非唯一的行名称:

38.40000 41.75200 44.38400 44.18400 45.37600 37.49600 41.36800 33.93600 38.00800  42.51200 46.49600  40.48000  45.40800  46.32800  43.78400  39.88800  38.84000  40.56800  42.03200 38.89185
45.53846 50.08462 39.91538 36.95385 34.96154 39.74615 38.01538 35.75385 35.54615  36.69231 35.20769  38.05385  39.29231  37.96923  37.30000  36.86923  39.19231  38.81538  43.69231 38.06400 
46.05176 41.69412 38.80000 37.75529 39.67529 39.07765 39.17647 38.24941 39.58588  38.63529 38.30588  41.87765  38.97412  40.13647  42.27294  38.24471  35.41647  40.80000  38.07059 42.11294
44.20000 43.42857 44.80000 35.20000 35.91429 37.82857 51.45714 44.68571 46.68571  48.74286 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
45.06909 41.09818 40.02909 42.50182 42.34909 39.84727 41.42909 40.47273 40.28000  40.51636 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
40.87407 39.27704 44.13630 43.25037 35.86667 37.30667 38.76148 40.74667 38.93333  43.16148 37.47259  37.73630  38.34370  39.00148  36.96889  37.76593  39.14667  37.92593  37.62963 38.89185

我想要这个虚拟数据的rownames是B,C,C,B,E2,E3。但是,我知道R不允许非唯一的rownames。我的完整数据集A,B,C,D,E1,E2,E3有七个可能的rowname类。

我试图在R中编写一个脚本(但是失败了),它采用了我的非唯一rownames的向量,并将数字添加到元素1,2,3 ...依此类推,具体取决于长度向量中的那个字符。

因此,对于这个虚拟数据,我将拥有的向量是B-1,C-1,C-2,B-2,E2-1,E3-1,我的最终矩阵将是:

B-1  38.40000 41.75200 44.38400 44.18400 45.37600 37.49600 41.36800 33.93600 38.00800  42.51200 46.49600  40.48000  45.40800  46.32800  43.78400  39.88800  38.84000  40.56800  42.03200 38.89185
C-1  45.53846 50.08462 39.91538 36.95385 34.96154 39.74615 38.01538 35.75385 35.54615  36.69231 35.20769  38.05385  39.29231  37.96923  37.30000  36.86923  39.19231  38.81538  43.69231 38.06400 
C-2  46.05176 41.69412 38.80000 37.75529 39.67529 39.07765 39.17647 38.24941 39.58588  38.63529 38.30588  41.87765  38.97412  40.13647  42.27294  38.24471  35.41647  40.80000  38.07059 42.11294
B-2  44.20000 43.42857 44.80000 35.20000 35.91429 37.82857 51.45714 44.68571 46.68571  48.74286 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
E2-1 45.06909 41.09818 40.02909 42.50182 42.34909 39.84727 41.42909 40.47273 40.28000  40.51636 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
E3-1 40.87407 39.27704 44.13630 43.25037 35.86667 37.30667 38.76148 40.74667 38.93333  43.16148 37.47259  37.73630  38.34370  39.00148  36.96889  37.76593  39.14667  37.92593  37.62963 38.89185

3 个答案:

答案 0 :(得分:1)

如果真的想要这样做,那么这将有效

uniqify_names <- function(names_vector) {
    names <- unique(names_vector)
    count_table <- rep(0, length(names))
    names(count_table) <- names # works because R has weird symbol lookup
    update_name <- function(name) {
        new_name <- paste0(name, ".", count_table[name])
        count_table[name] <<- count_table[name] + 1
        new_name
    }
    vapply(names_vector, update_name, FUN.VALUE = "character")
}

它的工作原理如下:

> non_unique_names <- c("A", "B", "A", "A", "B", "C", "A", "B", "C")
> uniqify_names(non_unique_names)
    A     B     A     A     B     C     A     B     C 
"A.0" "B.0" "A.1" "A.2" "B.1" "C.0" "A.3" "B.2" "C.1" 

您可以使用此向量中的rownames设置行名称。

答案 1 :(得分:1)

由于这个问题是如何:

  

获取非唯一rownames的向量,并将数字添加到元素1,2,3

我会忽略PCA部分(建议使用矩阵或根本不使用rownames)。

要生成想要的名称向量,您可以使用:

foo <- c("A", "B", "C", "C", "B", "E", "E")
paste0(foo, "-", sapply(seq_along(foo), function(x) sum(foo[1:x] == foo[x])))
[1] "A-1" "B-1" "C-1" "C-2" "B-2" "E-1" "E-2"

答案 2 :(得分:0)

你可以试试这个。

df <- data.frame(row_name = c('B','C','C','B','E2','E3'))

library(dplyr)

df <- df %>% 
  group_by(row_name) %>% 
  mutate(count = sequence(n()),
         unique_row_name = paste(row_name, count, sep="-"))

df$unique_row_name是你的候选人!