<p>Here a little way to do it. I don't think it is a perfect solution but it should work.
The trick here is to compare <code>patients_id</code> that are wrote with a <code>.</code> or with a <code>-</code>.
So, here is how I do (I modified your example in order to reflect several configurations):</p>

<pre class="lang-r prettyprint-override"><code># Reproducible example:
patient_id = c("TCGA.JY.A6FB.01A.11R.A336.31","TCGA.JY.A938.01A.11R.A37I.31","TCGA.L5.A4OS.01A.11R.A212.31","TCGA.AA.A400.01A.11R.A212.31")
ETV7 = c(1.79,5.38,5,6)
PLAUR = c(6.24,7.05,5,6)
SLC11A1 = c(3.7,3.12,5,6)

df = data.frame(ETV7,PLAUR, SLC11A1)
rownames(df) = patient_id

# OS data
Patient_ids = c("TCGA-IG-A4QS","TCGA-JY-A6FB","TCGA-JY-A938","TCGA-JY-A939","TCGA-JY-A93C","TCGA-JY-A93E","TCGA-L5-A4OJ","TCGA-L5-A4OS")
OS_time = c(3.93,61.23,35.33,22,23.5,25.56,21.3,59.4)
os_data = data.frame(Patient_ids,OS_time)
</code></pre>

<p>Now, I create a new column in <code>df</code> that will contains <code>patient_id</code> wrote with a <code>-</code>:</p>

<pre class="lang-r prettyprint-override"><code>library(rebus.base)
df$Patient_id = gsub(DOT,"-",rownames(df))
</code></pre>

<p>Now, I'm looking for common pattern between <code>df$Patient_id</code> and <code>os_data$Patient_ids</code>, find their matching row position and extract these lines:</p>

<pre class="lang-r prettyprint-override"><code>position = sapply(os_data$Patient_ids,function(x){grep(x,df$Patient_id)})
position = lapply(position,function(x){c(x,rep(NA,1-length(x)))})
position = unlist(position)
os_data$Position_matched = position
</code></pre>

<p>Now, we are creating a matrix <code>m</code> that we will fill with matching values</p>

<pre class="lang-r prettyprint-override"><code>m = data.frame(matrix(ncol = length(df),nrow = nrow(os_data)))
colnames(m) = colnames(df)

for(i in 1:length(position))
{
  if(is.na(position[i])){}
  else{
    m[i,] = df[position[i],]
  }
}
</code></pre>

<p>And now, we can bind both <code>os_data</code> and <code>m</code>: </p>

<pre class="lang-r prettyprint-override"><code>New_df = cbind(os_data,m)
</code></pre>

<p>And here the final output:</p>

<pre class="lang-r prettyprint-override"><code>&gt; New_df
   Patient_ids OS_time Position_matched ETV7 PLAUR SLC11A1                   Patient_id
1 TCGA-IG-A4QS    3.93               NA   NA    NA      NA                         &lt;NA&gt;
2 TCGA-JY-A6FB   61.23                1 1.79  6.24    3.70 TCGA-JY-A6FB-01A-11R-A336-31
3 TCGA-JY-A938   35.33                2 5.38  7.05    3.12 TCGA-JY-A938-01A-11R-A37I-31
4 TCGA-JY-A939   22.00               NA   NA    NA      NA                         &lt;NA&gt;
5 TCGA-JY-A93C   23.50               NA   NA    NA      NA                         &lt;NA&gt;
6 TCGA-JY-A93E   25.56               NA   NA    NA      NA                         &lt;NA&gt;
7 TCGA-L5-A4OJ   21.30               NA   NA    NA      NA                         &lt;NA&gt;
8 TCGA-L5-A4OS   59.40                3 5.00  5.00    5.00 TCGA-L5-A4OS-01A-11R-A212-31
</code></pre>

<p><strong>Warning:</strong> This script should work for some simple case like yours but take extra cautious to the <code>grep</code> output as the short <code>patient_id</code> term of the <code>os_data</code> could eventually generate several hits instead of one.</p>

CC BY-SA 4.0
2019-11-15T07:33:53.257Z
Sorry I got this Error in xj[i] : invalid subscript type 'list'
Zizogolu
CC BY-SA 4.0
2019-11-15T15:52:36.000Z
On which step of the code ?
dc37
CC BY-SA 4.0
2019-11-17T20:30:01.720Z
Thank you for your help, actually I got error in the step of your code
Zizogolu
CC BY-SA 4.0
2019-11-17T20:37:27.940Z
On which part ? please give me the part where you get an error and I will try to correct it for you. (also include the code you enter)
dc37
CC BY-SA 4.0
2019-11-17T20:38:59.327Z
in for loop I got that error
Zizogolu
CC BY-SA 4.0
2019-11-17T20:42:12.457Z
When you use the data example that I provided, did you get the same error ? also, did you install the packages `rebus.base` ?
dc37
CC BY-SA 4.0
2019-11-17T20:43:44.173Z
I have updated my main post with what I have typed in RStudio and what I got; Please have a look
Zizogolu
CC BY-SA 4.0
2019-11-17T20:49:44.983Z
Thanks ! I found the error. I forgot to to do `position = unlist(position)` before the `for` loop, I'm sorry about that. I have edited my code and now it should work.
dc37
CC BY-SA 4.0
2019-11-15T05:48:23.923Z
2019-11-17T20:48:37.737Z
2019-11-17T20:48:37.737Z
dc37
<p>I have a matrix in which sample IDs are in rows and genes are in column like below</p>

<pre><code>&gt; head(data)
                                 ETV7    PLAUR  SLC11A1
TCGA.JY.A6FB.01A.11R.A336.31 1.791167 6.241607 3.709848
TCGA.JY.A938.01A.11R.A37I.31 5.389175 7.048782 3.124435
                                BIRC3 TNFRSF1B
TCGA.JY.A6FB.01A.11R.A336.31 6.726991 5.758026
TCGA.JY.A938.01A.11R.A37I.31 8.097783 6.621900
&gt; 
</code></pre>

<p>In another matrix I have sample IDs, months to death and status while the order of sample IDs is not the same with my previous matrix like below</p>

<pre><code>Patient_ids OS_time  OS_event
TCGA-IG-A4QS    3.933333333 1
TCGA-JY-A6FB    61.23333333 0
TCGA-JY-A938    35.33333333 0
TCGA-JY-A939    22  0
TCGA-JY-A93C    23.5    0
TCGA-JY-A93E    25.56666667 0
TCGA-L5-A4OJ    21.3    0
TCGA-L5-A4OS    59.4    0
TCGA-L5-A8NM    7.866666667 1
TCGA-R6-A6KZ    5.133333333 1
TCGA-R6-A6XG    38.93333333 0
</code></pre>

<p>How I can bind OS_time and OS_event columns from second matrix to the first matrix keeping the right sample order?</p>

<hr>

<blockquote>
  <p>EDITED</p>
</blockquote>

<hr>

<pre><code>&gt; # Reproducible example:
&gt; patient_id = c("TCGA.JY.A6FB.01A.11R.A336.31","TCGA.JY.A938.01A.11R.A37I.31","TCGA.L5.A4OS.01A.11R.A212.31","TCGA.AA.A400.01A.11R.A212.31")
&gt; ETV7 = c(1.79,5.38,5,6)
&gt; PLAUR = c(6.24,7.05,5,6)
&gt; SLC11A1 = c(3.7,3.12,5,6)
&gt; df = data.frame(ETV7,PLAUR, SLC11A1)
&gt; rownames(df) = patient_id
&gt; # OS data
&gt; Patient_ids = c("TCGA-IG-A4QS","TCGA-JY-A6FB","TCGA-JY-A938","TCGA-JY-A939","TCGA-JY-A93C","TCGA-JY-A93E","TCGA-L5-A4OJ","TCGA-L5-A4OS")
&gt; OS_time = c(3.93,61.23,35.33,22,23.5,25.56,21.3,59.4)
&gt; os_data = data.frame(Patient_ids,OS_time)
&gt; library(rebus.base)
&gt; df<span class="math-container">$Patient_id = gsub(DOT,"-",rownames(df))
&gt; position = sapply(os_data$</span>Patient_ids,function(x){grep(x,df<span class="math-container">$Patient_id)})
&gt; position = lapply(position,function(x){c(x,rep(NA,1-length(x)))})
&gt; os_data$</span>Position_matched = unlist(position)
&gt; m = data.frame(matrix(ncol = length(df),nrow = nrow(os_data)))
&gt; colnames(m) = colnames(df)
&gt; for(i in 1:length(position))
+ {
+   if(is.na(position[i])){}
+   else{
+     m[i,] = df[position[i],]
+   }
+ }
Error in xj[i] : invalid subscript type 'list'
&gt; 
</code></pre>

<r>
Binding columns with matched elements
# Binding columns with matched elements

(Asked by: username_0 on 2019-11-15)

I have a matrix in which sample IDs are in rows and genes are in column like below
> head(data)
                                 ETV7    PLAUR  SLC11A1
TCGA.JY.A6FB.01A.11R.A336.31 1.791167 6.241607 3.709848
TCGA.JY.A938.01A.11R.A37I.31 5.389175 7.048782 3.124435
                                BIRC3 TNFRSF1B
TCGA.JY.A6FB.01A.11R.A336.31 6.726991 5.758026
TCGA.JY.A938.01A.11R.A37I.31 8.097783 6.621900
> 

In another matrix I have sample IDs, months to death and status while the order of sample IDs is not the same with my previous matrix like below
Patient_ids OS_time  OS_event
TCGA-IG-A4QS    3.933333333 1
TCGA-JY-A6FB    61.23333333 0
TCGA-JY-A938    35.33333333 0
TCGA-JY-A939    22  0
TCGA-JY-A93C    23.5    0
TCGA-JY-A93E    25.56666667 0
TCGA-L5-A4OJ    21.3    0
TCGA-L5-A4OS    59.4    0
TCGA-L5-A8NM    7.866666667 1
TCGA-R6-A6KZ    5.133333333 1
TCGA-R6-A6XG    38.93333333 0

How I can bind OS_time and OS_event columns from second matrix to the first matrix keeping the right sample order?


EDITED


> # Reproducible example:
> patient_id = c("TCGA.JY.A6FB.01A.11R.A336.31","TCGA.JY.A938.01A.11R.A37I.31","TCGA.L5.A4OS.01A.11R.A212.31","TCGA.AA.A400.01A.11R.A212.31")
> ETV7 = c(1.79,5.38,5,6)
> PLAUR = c(6.24,7.05,5,6)
> SLC11A1 = c(3.7,3.12,5,6)
> df = data.frame(ETV7,PLAUR, SLC11A1)
> rownames(df) = patient_id
> # OS data
> Patient_ids = c("TCGA-IG-A4QS","TCGA-JY-A6FB","TCGA-JY-A938","TCGA-JY-A939","TCGA-JY-A93C","TCGA-JY-A93E","TCGA-L5-A4OJ","TCGA-L5-A4OS")
> OS_time = c(3.93,61.23,35.33,22,23.5,25.56,21.3,59.4)
> os_data = data.frame(Patient_ids,OS_time)
> library(rebus.base)
> df$Patient_id = gsub(DOT,"-",rownames(df))
> position = sapply(os_data$Patient_ids,function(x){grep(x,df$Patient_id)})
> position = lapply(position,function(x){c(x,rep(NA,1-length(x)))})
> os_data$Position_matched = unlist(position)
> m = data.frame(matrix(ncol = length(df),nrow = nrow(os_data)))
> colnames(m) = colnames(df)
> for(i in 1:length(position))
+ {
+   if(is.na(position[i])){}
+   else{
+     m[i,] = df[position[i],]
+   }
+ }
Error in xj[i] : invalid subscript type 'list'
>

**Tags:** r

---

## 1 answers

### Answer by username_1 on 2019-11-15. Score: 2 | Accepted answer

Here a little way to do it. I don't think it is a perfect solution but it should work.
The trick here is to compare patients_id that are wrote with a . or with a -.
So, here is how I do (I modified your example in order to reflect several configurations):
# Reproducible example:
patient_id = c("TCGA.JY.A6FB.01A.11R.A336.31","TCGA.JY.A938.01A.11R.A37I.31","TCGA.L5.A4OS.01A.11R.A212.31","TCGA.AA.A400.01A.11R.A212.31")
ETV7 = c(1.79,5.38,5,6)
PLAUR = c(6.24,7.05,5,6)
SLC11A1 = c(3.7,3.12,5,6)

df = data.frame(ETV7,PLAUR, SLC11A1)
rownames(df) = patient_id

# OS data
Patient_ids = c("TCGA-IG-A4QS","TCGA-JY-A6FB","TCGA-JY-A938","TCGA-JY-A939","TCGA-JY-A93C","TCGA-JY-A93E","TCGA-L5-A4OJ","TCGA-L5-A4OS")
OS_time = c(3.93,61.23,35.33,22,23.5,25.56,21.3,59.4)
os_data = data.frame(Patient_ids,OS_time)

Now, I create a new column in df that will contains patient_id wrote with a -:
library(rebus.base)
df$Patient_id = gsub(DOT,"-",rownames(df))

Now, I'm looking for common pattern between df$Patient_id and os_data$Patient_ids, find their matching row position and extract these lines:
position = sapply(os_data$Patient_ids,function(x){grep(x,df$Patient_id)})
position = lapply(position,function(x){c(x,rep(NA,1-length(x)))})
position = unlist(position)
os_data$Position_matched = position

Now, we are creating a matrix m that we will fill with matching values
m = data.frame(matrix(ncol = length(df),nrow = nrow(os_data)))
colnames(m) = colnames(df)

for(i in 1:length(position))
{
  if(is.na(position[i])){}
  else{
    m[i,] = df[position[i],]
  }
}

And now, we can bind both os_data and m: 
New_df = cbind(os_data,m)

And here the final output:
> New_df
   Patient_ids OS_time Position_matched ETV7 PLAUR SLC11A1                   Patient_id
1 TCGA-IG-A4QS    3.93               NA   NA    NA      NA                         
2 TCGA-JY-A6FB   61.23                1 1.79  6.24    3.70 TCGA-JY-A6FB-01A-11R-A336-31
3 TCGA-JY-A938   35.33                2 5.38  7.05    3.12 TCGA-JY-A938-01A-11R-A37I-31
4 TCGA-JY-A939   22.00               NA   NA    NA      NA                         
5 TCGA-JY-A93C   23.50               NA   NA    NA      NA                         
6 TCGA-JY-A93E   25.56               NA   NA    NA      NA                         
7 TCGA-L5-A4OJ   21.30               NA   NA    NA      NA                         
8 TCGA-L5-A4OS   59.40                3 5.00  5.00    5.00 TCGA-L5-A4OS-01A-11R-A212-31

Warning: This script should work for some simple case like yours but take extra cautious to the grep output as the short patient_id term of the os_data could eventually generate several hits instead of one.

> **Comments:**
> **username_0**: Sorry I got this Error in xj[i] : invalid subscript type 'list'  
> **username_1**: On which step of the code ?  
> **username_0**: Thank you for your help, actually I got error in the step of your code  
> **username_1**: On which part ? please give me the part where you get an error and I will try to correct it for you. (also include the code you enter)  
> **username_0**: in for loop I got that error  
> **username_1**: When you use the data example that I provided, did you get the same error ? also, did you install the packages `rebus.base` ?  
> **username_0**: I have updated my main post with what I have typed in RStudio and what I got; Please have a look  
> **username_1**: Thanks ! I found the error. I forgot to to do `position = unlist(position)` before the `for` loop, I'm sorry about that. I have edited my code and now it should work.  

---

