Reputation: 267
I have a data.frame
object in R , for which I want to have non-unique row names:
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
The rownames I want for this dummy data would be B,C,C,B,E2,E3
. However, I am aware that R does not allow non-unique rownames. I have seven possible rowname classes for my complete dataset, A,B,C,D,E1,E2,E3
.
I was trying to write a script in R (but failing), that takes a vector of my non-unique rownames, and adds numbers to the elements 1,2,3... so on, depending on the length of that character in the vector.
Thus for this dummy data, the vector I would have is B-1,C-1,C-2,B-2,E2-1,E3-1
, and my final matrix would be:
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
Upvotes: 2
Views: 836
Reputation: 28379
Since this question is how to:
take a vector of non-unique rownames and add numbers to the elements 1,2,3
I will ignore the PCA part (recommendations to use matrix or not to use rownames at all).
To generate wanted vector of names you can use this:
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"
Upvotes: 1
Reputation: 11985
You can try this.
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
is your candidate!
Upvotes: 0
Reputation: 1826
If you really want to do this, then this will work
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")
}
It works like this:
> 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"
You can set the row names using rownames
from this vector.
Upvotes: 1