how to use make.names commnad in r while using rbind function
1
1
Entering edit mode
21 months ago
rheab1230 ▴ 140

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 • 2.2k views
ADD COMMENT
0
Entering edit mode

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()?

ADD REPLY
2
Entering edit mode
21 months ago
zx8754 12k

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
ADD COMMENT
0
Entering edit mode

yes, this work. thank you

ADD REPLY

Login before adding your answer.

Traffic: 2018 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6