Entering edit mode
                    7.4 years ago
        Spacebio
        
    
        ▴
    
    200
    Hello,
I have two different dfs looking like the example below. df1 displays the name of a group of pathways as df2 shows the category of the pathway in the same order they appear on df1
> df1:
Path_1                                           Path_2
Amphoterin signaling                             Antigen presentation
Antigen presentation                             Death Domain receptors & caspases in apoptosis
Regulation of angiogenesis                       Apoptosis stimulation by external signals
Blood vessel morphogenesis                       Regulation of angiogenesis
Cartilage development                            Blood vessel morphogenesis
Apoptosis stimulation by external signals        Cartilage development
Death Domain receptors & caspases in apoptosis   Amphoterin signaling
> df2:
Type_1                     Type_2
Inflammation               Immune response
Immune response            Signal transduction
Development                Apoptosis and survival
Development                Development
Development                Development
Apoptosis and survival     Development
Signal transduction        Inflammation
I'd like to obtain a unique df displaying both columns like this:
> df_all:
df_all_1
Amphoterin signaling_Inflammation
Antigen presentation_Immune response
Regulation of angiogenesis_Development
Blood vessel morphogenesis_Development
Cartilage development_Development
Apoptosis stimulation by external signals_Apoptosis and survival
Death Domain receptors & caspases in apoptosis_Signal transduction
df_all_2
Antigen presentation_Immune response
Death Domain receptors & caspases in apoptosis_Signal transduction
Apoptosis stimulation by external signals_Apoptosis and survival
Regulation of angiogenesis_Development
Blood vessel morphogenesis_Development
Cartilage development_Development
Amphoterin signaling_Inflammation
I tried with this code:
df_all <- merge(data.frame(df1, row.names = NULL), data.frame(df2, row.names = NULL), by = 0, all = T)[-1]
but this is just merging all the columns together without alternating.
Any suggestions? Preferably base R
Output will be stored in a third dataframe (df3) and each column from two data frames will be concatenated. It is a blind concatenation assuming that column 1 of df1 has exact rows as column 1 of df2 and they match. Number of columns and number of rows of each data frame (df1, df2) match with resultant data frame (df3)
or
output:
The loop works really fast, thank you so much!!
To get column names as df_all_1, df_all_2, use following code: