Merge two columns dataframes
2
0
Entering edit mode
4.6 years ago

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.

gene R • 885 views
ADD COMMENT
0
Entering edit mode
ADD REPLY
2
Entering edit mode
4.6 years ago
ATpoint 82k

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.

ADD COMMENT
0
Entering edit mode
4.6 years ago
nterhoeven ▴ 120

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

ADD COMMENT

Login before adding your answer.

Traffic: 3003 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6