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.
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.
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.
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 .
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) .
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With