Question: Merge two columns dataframes
0
gravatar for samira.reuscher
12 months ago by
samira.reuscher0 wrote:

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.

R gene • 277 views
ADD COMMENTlink modified 12 months ago by zx87549.6k • written 12 months ago by samira.reuscher0

Relevant SO post:

ADD REPLYlink written 12 months ago by zx87549.6k
2
gravatar for ATpoint
12 months ago by
ATpoint39k
Germany
ATpoint39k wrote:

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 COMMENTlink modified 12 months ago • written 12 months ago by ATpoint39k
0
gravatar for nterhoeven
12 months ago by
nterhoeven120
nterhoeven120 wrote:

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 COMMENTlink written 12 months ago by nterhoeven120
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1202 users visited in the last hour