This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to use make.names commnad in r while using rbind function

Hello everyone,

I have two dataframes: df1 and df2. I want to combine them using rbind function. These two dataframe have some overlapping rownames. When combining it adds a value to the overlapping rownames.

For example:

df1 has one row as rs119151

df2 also has one row as rs119151

After combining its coming as

rs119151
rs1191511

I want it to show as:

rs119151
rs119151.1

and so on if we have more same rows for this value.

I am trying to use make.names command but its not working for me?

new <- make.names(rbind(df1,df2),unique=TRUE)

Is there any other way to do it?

r

Why/how isn't your command working? I'm guessing because rbind() already makes the names unique so the make.names() function has nothing to rename. Try

new <- make.names(rbind(df1,df2,make.row.names = FALSE),unique=TRUE)

as a sort of ugly workaround. If anyone knows how to modify the rbind() make.row.names behavior I'm all ears!

But I also would think about this and make sure you really intend to be doing what you are doing. If the rownames are the same then why not just cbind()?

1 answer

make.names works on a vector, not a dataframe.

We can rbind, then reassign fixed rownames, try this example:

d1 <- data.frame(x = 1:2, row.names = c("rs1", "rs2"))
d2 <- data.frame(x = 4:5, row.names = c("rs1", "rs3"))

res <- rbind(d1, d2)
#      x
# rs1  1
# rs2  2
# rs11 4
# rs3  5
rownames(res) <- make.unique(c(rownames(d1), rownames(d2)))
res
#       x
# rs1   1
# rs2   2
# rs1.1 4
# rs3   5

Log in to answer this question.