Thank you very much, I'm gonna try. Will this keep the names of the other genes that I already have? Because only some of the rows are "unknown", for other I am fine with the gene name.
Good afternoon,
I'm facing an issue with R. I have a dataframe with some genes, some of them are "n/a", I successfully changed "n/a" to "unknown" using the code: genes[is.na(genes)] <- "unknown"
Now I want to create a graph but I need unique ID for "unknown", as "unknown1", "unknown2" etc.
How can I instruct rstudio to do that progressing numbering of "unknown"?
Thank you in advance
2 answers
Use make.names with unique = TRUE on the genes data frame. See ?make.names, e.g:
rownames(df) <- make.names(genes, unique = TRUE)
If you use this code, any duplicated name in your rownames will be treated like this. Also, non-word characters (e.g. '-', space) will be replaced with '.' It is in fact good practice to have rownames that are formed like this otherwise you will get problems when indexing content by row names. You could keep the original gene vector as another column in your data frame.
To add sequential numbers to duplicated elements in a vector, you can use the rowid function from the data.table package. This function generates unique identifiers for each occurrence of duplicated values, making it easy to distinguish between them.:
x <- c("a", "b", "a", "c", "c", "c")
paste(x, data.table::rowid(x), sep = ".")
# [1] "a.1" "b.1" "a.2" "c.1" "c.2" "c.3"
To ensure that all elements in a vector have unique names, you can use the make.unique function. This function appends a suffix to duplicated elements to make them unique.
make.unique(x)
# [1] "a" "b" "a.1" "c" "c.1" "c.2"
Log in to answer this question.