uncool
uncool

Reputation: 2703

dplyr left_join() by rownames

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.

Upvotes: 25

Views: 44108

Answers (2)

jarauh
jarauh

Reputation: 2006

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=c("rowname" = "Symbol"))

Upvotes: 22

Marcin
Marcin

Reputation: 8044

Does this help?

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

Upvotes: 9

Related Questions