<p>If you're still looking for an answer on this, I would use the <code>gather</code> and <code>spread</code> functions in the <code>tidyr</code> package (part of the <code>tidyverse</code> suite of packages - which also includes <code>dplyr</code> and <code>ggplot2</code>:</p>

<pre><code>library(tidyverse) 
df2_long &lt;- gather(PATIENT_ID, value, -Hugo_Symbol)
df1 %&gt;% 
  left_join(df2_long) %&gt;%   # merges the 2 data frames together
  spread(Hugo_Symbol, value)
</code></pre>

CC BY-SA 3.0
2018-04-09T04:18:06.873Z
2018-04-09T04:18:06.873Z
Melissa Key
<p>You can take the PATIENT_ID from the first dataframe and use them to select from your second dataframe. Than transpose, so something like this:</p>

<pre><code>ID_Names &lt;- First_df$PATIENT_ID

rownames(Second_df) &lt;- Second_df$Hugo_Symbol

Selected_df &lt;- Second_df[, ID_Names]

Expected_df &lt;- t(Selected_df)
</code></pre>

CC BY-SA 3.0
2018-03-23T05:14:54.570Z
well i m running into an error "Error: Columns `TCGA-AB-2831`, `TCGA-AB-2907`, `TCGA-AB-2922` not found" may be one problem i think is my ID_Names vector is in character , but the column i m trying to map are in numeric ...any suggestion
kcm
CC BY-SA 3.0
2018-03-23T08:45:28.120Z
Are you sure these three patient_IDs are in the second file? You select from the header which is not numeric.
benn
CC BY-SA 3.0
2018-03-23T10:38:21.907Z
i mean all my header in the second data frame are numeric...so do i need to convert them to character if yes how do i do that i tried converting them to character it changes my data frame
kcm
CC BY-SA 3.0
2018-03-23T11:52:15.093Z
Can you show: `class(colnames(second_df))`, I see characters in your colnames, so I doubt that you are right about numeric header. Are you sure every ID is in the colnames of your second data frame?
benn
CC BY-SA 3.0
2018-03-23T12:47:21.630Z
The error is pretty clear, you miss the three IDs in your second df. Check the colnames and look for the specific three IDs in the error.
benn
CC BY-SA 3.0
2018-03-24T05:35:46.170Z
[1] "character" my first colname is character as i see and rest as numeric when i hover my cursor over the rest
kcm
CC BY-SA 3.0
2018-03-24T05:53:14.967Z
yeah now its solved..,i think those no ID in my second df, but now i transposed the data frame but the gene names are not coming..
kcm
CC BY-SA 3.0
2018-03-24T10:20:22.293Z
Make rownames with the gene names for the second df, before you select. Something like this: `rownames(second_df) <- second_df$Hugo_Symbol`. If you then select the columns the gene names are stored as rownames.
benn
CC BY-SA 3.0
2018-03-22T13:13:36.677Z
2018-03-25T10:24:32.757Z
2018-03-25T10:24:32.757Z
benn
<p>My first data frame whose first column i intend to use which is PATIENT_ID</p>

<pre><code>PATIENT_ID  SEX RACE    FAB AGE BM_BLAST_PERCENTAGE WBC
TCGA-AB-2805    Male    WHITE   M0  77  67  92
TCGA-AB-2822    Male    WHITE   M0  65  99  2.9
TCGA-AB-2831    Male    WHITE   M0  59  61  1.4
TCGA-AB-2890    Male    NH/C    M0  69  60  6.1
TCGA-AB-2907    Male    WHITE   M0  69  82  1.6
TCGA-AB-2913    Male    WHITE   M0  61  95  61.6
TCGA-AB-2922    Male    WHITE   M0  83  75  77.9
TCGA-AB-2936    Female  NH/C    M0  60  50  2.1
</code></pre>

<p>My second data frame where i have the respective patient ID as individual column with expression values </p>

<pre><code>Hugo_Symbol TCGA-AB-2803    TCGA-AB-2805    TCGA-AB-2806    TCGA-AB-2807    TCGA-AB-2808
A1BG    0   0   0   0   0
A1CF    0   0   0   0   0
A2BP1   71.4095 48.5475 50.8339 45.7498 47.1328
A2LD1   2.0576  3.3467  5.7288  0.993   5.4988
A2ML1   467.0782    16.0643 7.0019  343.5948    255.9152
A2M 11.3169 0.6693  17.823  116.1867    182.2467
A4GALT  0   2.6774  1.2731  4.9652  0.7855
A4GNT   0   0   0   0   0
AAA1    514.4033    532.7979    504.1375    697.1202    443.8335
AAAS    0   0   0   0   0
AACSL   190.3292    74.2972 84.6595 559.0864    317.3606
AACS    0   0   0   1.9861  0
AADACL2 0   0   0   0   0
AADACL3 0   0   0   0   0
AADACL4 1.0288  0   0.6365  0   0
</code></pre>

<p>What I tried so far transposing the second data frame so that i can get the individual column as my first column ,and subset by using dplyr library but so far im not able to ..any help or suggestion how to do it would be highly appreciable </p>

<p>I would like to have my expected data frame as such </p>

<pre><code>PATIENT_ID  Gene B  GeneA   GeneC   GeneA   GeneA 
TCGA-AB-2803-03 0   0   0   92.5926 876.5432
TCGA-AB-2805-03 0.6693  1.3387  0   92.6305 421.6867
TCGA-AB-2806-03 0   14.1884 16.3654 119.599 625.0796
TCGA-AB-2807-03 2.006   9.3247  13.5154 178.4211    537.2393
TCGA-AB-2808-03 0   42.3016 28.3975 170.2042    653.5742
TCGA-AB-2810-03 0   1.5668  8.9665  152.0747    1087.5648
TCGA-AB-2811-03 0   0   6.9556  30.3103 1414.6602
TCGA-AB-2812-03 0   14.3289 11.9466 120.3591    962.5575
TCGA-AB-2813-03 0   3.4938  1.3813  51.1883 594.7593
TCGA-AB-2814-03 0   56.8659 118.7051    87.1817 472.6912
TCGA-AB-2815-03 0   2.0692  3.6986  104.6575    502.5234
</code></pre>

CC BY-SA 3.0
2018-03-22T11:47:11.377Z
okay so I what im expecting is I would like to have total 18 patient which are in first data frame my patient ID so I would like to use the patient ID and  use the same to subset the 18 patient from my second data frame with their expression values my second data frame 22000*174 in dimension .so my expected data fram would be 22000*18 , so i need my rows as my genes and columns as my patient sample ID..
kcm
CC BY-SA 3.0
2018-03-22T13:31:12.013Z
Are you sure that those numbers on that expression matrix are hugo symbols?? I highly doubt it. BTW I think this is not about bioinformatics...
llrs
CC BY-SA 3.0
2018-03-23T05:12:07.597Z
well i just put it as since it didn't contain any symbol but entrez ID
kcm
<r>
Subset data frame by similar elements from rows and column between two data frames
# Subset data frame by similar elements from rows and column between two data frames

(Asked by: username_0 on 2018-03-22)

My first data frame whose first column i intend to use which is PATIENT_ID
PATIENT_ID  SEX RACE    FAB AGE BM_BLAST_PERCENTAGE WBC
TCGA-AB-2805    Male    WHITE   M0  77  67  92
TCGA-AB-2822    Male    WHITE   M0  65  99  2.9
TCGA-AB-2831    Male    WHITE   M0  59  61  1.4
TCGA-AB-2890    Male    NH/C    M0  69  60  6.1
TCGA-AB-2907    Male    WHITE   M0  69  82  1.6
TCGA-AB-2913    Male    WHITE   M0  61  95  61.6
TCGA-AB-2922    Male    WHITE   M0  83  75  77.9
TCGA-AB-2936    Female  NH/C    M0  60  50  2.1

My second data frame where i have the respective patient ID as individual column with expression values 
Hugo_Symbol TCGA-AB-2803    TCGA-AB-2805    TCGA-AB-2806    TCGA-AB-2807    TCGA-AB-2808
A1BG    0   0   0   0   0
A1CF    0   0   0   0   0
A2BP1   71.4095 48.5475 50.8339 45.7498 47.1328
A2LD1   2.0576  3.3467  5.7288  0.993   5.4988
A2ML1   467.0782    16.0643 7.0019  343.5948    255.9152
A2M 11.3169 0.6693  17.823  116.1867    182.2467
A4GALT  0   2.6774  1.2731  4.9652  0.7855
A4GNT   0   0   0   0   0
AAA1    514.4033    532.7979    504.1375    697.1202    443.8335
AAAS    0   0   0   0   0
AACSL   190.3292    74.2972 84.6595 559.0864    317.3606
AACS    0   0   0   1.9861  0
AADACL2 0   0   0   0   0
AADACL3 0   0   0   0   0
AADACL4 1.0288  0   0.6365  0   0

What I tried so far transposing the second data frame so that i can get the individual column as my first column ,and subset by using dplyr library but so far im not able to ..any help or suggestion how to do it would be highly appreciable 
I would like to have my expected data frame as such 
PATIENT_ID  Gene B  GeneA   GeneC   GeneA   GeneA 
TCGA-AB-2803-03 0   0   0   92.5926 876.5432
TCGA-AB-2805-03 0.6693  1.3387  0   92.6305 421.6867
TCGA-AB-2806-03 0   14.1884 16.3654 119.599 625.0796
TCGA-AB-2807-03 2.006   9.3247  13.5154 178.4211    537.2393
TCGA-AB-2808-03 0   42.3016 28.3975 170.2042    653.5742
TCGA-AB-2810-03 0   1.5668  8.9665  152.0747    1087.5648
TCGA-AB-2811-03 0   0   6.9556  30.3103 1414.6602
TCGA-AB-2812-03 0   14.3289 11.9466 120.3591    962.5575
TCGA-AB-2813-03 0   3.4938  1.3813  51.1883 594.7593
TCGA-AB-2814-03 0   56.8659 118.7051    87.1817 472.6912
TCGA-AB-2815-03 0   2.0692  3.6986  104.6575    502.5234

**Tags:** r
> **Comments:**
> **username_0**: okay so I what im expecting is I would like to have total 18 patient which are in first data frame my patient ID so I would like to use the patient ID and  use the same to subset the 18 patient from my second data frame with their expression values my second data frame 22000*174 in dimension .so my expected data fram would be 22000*18 , so i need my rows as my genes and columns as my patient sample ID..  
> **username_1**: Are you sure that those numbers on that expression matrix are hugo symbols?? I highly doubt it. BTW I think this is not about bioinformatics...  
> **username_0**: well i just put it as since it didn't contain any symbol but entrez ID  

---

## 2 answers

### Answer by username_2 on 2018-04-09. Score: 1 | Accepted answer

If you're still looking for an answer on this, I would use the gather and spread functions in the tidyr package (part of the tidyverse suite of packages - which also includes dplyr and ggplot2:
library(tidyverse) 
df2_long <- gather(PATIENT_ID, value, -Hugo_Symbol)
df1 %>% 
  left_join(df2_long) %>%   # merges the 2 data frames together
  spread(Hugo_Symbol, value)


---

### Answer by username_3 on 2018-03-22. Score: 4

You can take the PATIENT_ID from the first dataframe and use them to select from your second dataframe. Than transpose, so something like this:
ID_Names <- First_df$PATIENT_ID

rownames(Second_df) <- Second_df$Hugo_Symbol

Selected_df <- Second_df[, ID_Names]

Expected_df <- t(Selected_df)

> **Comments:**
> **username_0**: well i m running into an error "Error: Columns `TCGA-AB-2831`, `TCGA-AB-2907`, `TCGA-AB-2922` not found" may be one problem i think is my ID_Names vector is in character , but the column i m trying to map are in numeric ...any suggestion  
> **username_3**: Are you sure these three patient_IDs are in the second file? You select from the header which is not numeric.  
> **username_0**: i mean all my header in the second data frame are numeric...so do i need to convert them to character if yes how do i do that i tried converting them to character it changes my data frame  
> **username_3**: Can you show: `class(colnames(second_df))`, I see characters in your colnames, so I doubt that you are right about numeric header. Are you sure every ID is in the colnames of your second data frame?  
> **username_3**: The error is pretty clear, you miss the three IDs in your second df. Check the colnames and look for the specific three IDs in the error.  
> **username_0**: [1] "character" my first colname is character as i see and rest as numeric when i hover my cursor over the rest  
> **username_0**: yeah now its solved..,i think those no ID in my second df, but now i transposed the data frame but the gene names are not coming..  
> **username_3**: Make rownames with the gene names for the second df, before you select. Something like this: `rownames(second_df) <- second_df$Hugo_Symbol`. If you then select the columns the gene names are stored as rownames.  

---

