dplyr left_join() by rownames

2020-06-08 13:36发布

I'm using the dplyrs function left_join to combine two data.frames.

Now I would like to manually join them by using the rownames in the left data.frame and the appropriate column name in the right data.frame of left_join, but I get the error:

Error: unexpected '=' in "dplyr::left_join(x.tsummary, sto.info, by = c(rownames(x.tsummary) ="

My code

> dplyr::left_join(x.tsummary, sto.info, by = c(rownames(x.tsummary) = 'Symbol'))

Is this even possible? In the documentation it says that I should specify column names, but it would be great if I could join on rownames for the left data.frame.

标签: r dplyr
2条回答
霸刀☆藐视天下
2楼-- · 2020-06-08 13:52

Does this help?

dplyr::left_join(x.tsummary %>%
                                    mutate(Symbol = rownames(x.tsummary)),
                                 sto.info,
                                 by = 'Symbol')
查看更多
叛逆
3楼-- · 2020-06-08 13:55

dplyr now recommends to use tibble::rownames_to_column (dplyr also has a function add_rownames, which is deprecated in favor of tibble::rownames_to_column). For example:

library(tibble)
library(dplyr)
left_join(rownames_to_column(x.tsummary), sto.info, by = ("rowname" = "Symbol"))
查看更多
登录 后发表回答