Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr left_join() by rownames

Tags:

r

dplyr

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.

like image 331
uncool Avatar asked Dec 30 '15 11:12

uncool


People also ask

What does Left_join do in R?

A left join in R is a merge operation between two data frames where the merge returns all of the rows from one table (the left side) and any matching rows from the second table. A left join in R will NOT return values of the second table which do not already exist in the first table.

How do I merge two Dataframes in Rownames in R?

The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.

How do I join tables in Dplyr?

To join by different variables on x and y , use a named vector. For example, by = c("a" = "b") will match x$a to y$b . To join by multiple variables, use a vector with length > 1. For example, by = c("a", "b") will match x$a to y$a and x$b to y$b .

How do I join Dplyr Dataframes?

Joins with dplyr. dplyr uses SQL database syntax for its join functions. A left join means: Include everything on the left (what was the x data frame in merge() ) and all rows that match from the right (y) data frame. If the join columns have the same name, all you need is left_join(x, y) .


1 Answers

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"))
like image 170
jarauh Avatar answered Sep 19 '22 11:09

jarauh