Hello, i have a question concerning two dataframe that should be merged. To make it more understandable, I broke it down on a simple example.
tabelle1 <- data.frame(genID = c("a","b","c"),
log2FC = c(11,22,33))
tabelle2 <- data.frame(genID = c("b","c","d"),
log2FC = c(44,55,66))
The new dataframe should consist of 3 columns (genID, log2FCtabelle1, log2FCtabelle2) and four rows (a,b,c,d). Missing values should be marked with NA ...
I hope someone can hep me! Thanks.
2 answers
You can use the base function merge like:
merge(tabelle1, tabelle2, by.x = "genID", by.y = "genID", all = TRUE)
genID log2FC.x log2FC.y
a 11 NA
b 22 44
c 33 55
d NA 66
The by.x/y arguments take the column name of the respective data.frames that shall be used for merging. all=TRUE means to replace missing values by NA.
You can do this with left_join() from the tidyverse package
You should rename the log2FC columns to different names and then run left_join(tabelle1,tabelle2). This will merge the two data frames by the common column (genID).
Take a look at https://stat545.com/join-cheatsheet.html for more info about the different join commands
Log in to answer this question.
Relevant SO post: