title: Extracting all rows from multiple columns with specific value from dataframe in R
body: <p>I have a dataframe called <code>all_genes</code> that has 157 columns in total, the first column being a <code>genes</code> column containing gene names. The columns of interests are from 50th to 157th with 2-step (<code>50</code>, <code>52</code>, <code>54</code>, <code>56</code>, etc ...) which are the sample's names. These columns have three types of values: <code>1</code>, <code>2</code> or <code>3</code>, knowing that for the same row (same gene), we could have the three types of values for different samples. </p>
  
  <p>For example, the row of gene <code>X</code> has a value of <code>1</code> in column 50th column but value of <code>2</code> for 52nd column.</p>
  
  <p>What I wish is to extract all rows from the even columns depending on these values. To get a better idea, here's how the dataframe looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/oUBCq.png" rel="nofollow noreferrer">Original dataframe</a></p>
  
  <p>Now, I have written this code to extract, for example, rows of value <code>1</code>:</p>
  
  <pre><code># extracting rows of value "1" from column 50 to 157, by taking into account only the even columns
  df &lt;- all_genes[which(all_genes[, seq(50, 157, 2)] == 1), ] 
  
  # removing NAs if all the rows are NAs from columns 50 to 157
  df &lt;- df[rowSums(is.na(df[, 50:157])) != ncol(df[, 50:157]), ]
  </code></pre>
  
  <p>However, what I get is the following:</p>
  
  <p><a href="https://i.stack.imgur.com/uWySO.png" rel="nofollow noreferrer">Output of the above code</a></p>
  
  <p>As you can see, the first column contains values that are all equal to <code>1</code> but if you look at other columns, you see values of <code>2</code> (and <code>3</code>). I think my code is only looking at the fiftieth column and neglecting the possibility to get different values than 1 for the 50th column because for the same gene, we can have a value of <code>2</code> in the 50th column but <code>1</code> for the 52nd column. To confirm that, I checked the possibility (please copy-paste the following link since I don't have enough reputation):</p>
  
  <p>i.stack.imgur.com/rZQ2E.png</p>
  
  <p>Could you please tell me if my code is working correctly or should I change something ?</p>
  
  <p>The same thing happens if I change in my code the value of <code>1</code> to <code>2</code>. I will still get values of <code>2</code> in the 50th column but all kind of values in the other columns.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong>
  As requested by @tobiasegli_te, here's a small reproducible dataframe:</p>
  
  <pre><code>structure(list(`#00e41e6a-9fe7-44f9-978b-7b05b179506a` = c(1, 
  1, NA, NA, NA, NA, NA, NA, NA, 1, 2, 1, NA, NA, 2, NA, 3, 1, 
  1, NA, NA, NA, 2, NA, 1, NA, NA, NA, NA, 1, 1, 1, NA, NA, 1, 
  NA, NA), `#aca312ab-6dbd-4183-8b22-8f37834f3426` = c(NA, NA, 
  NA, 1, NA, 1, NA, 2, 1, NA, 2, 1, 1, 1, NA, NA, NA, 1, 1, 1, 
  NA, 1, 2, 1, NA, 1, NA, 1, NA, NA, 1, NA, 1, NA, 1, 1, 1), `#0730216b-c201-443c-9092-81e23fd13c6c` = c(NA, 
  NA, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, 
  NA, NA, NA, 1, NA, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA, NA, 
  2, 1, NA, NA), `#acd5ceef-c5cf-4e95-9394-c50fdbc70c8d` = c(NA, 
  NA, 2, NA, 2, NA, 2, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2, NA, 
  NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, 
  1, NA, NA)), .Names = c("#00e41e6a-9fe7-44f9-978b-7b05b179506a", 
  "#aca312ab-6dbd-4183-8b22-8f37834f3426", "#0730216b-c201-443c-9092-81e23fd13c6c", 
  "#acd5ceef-c5cf-4e95-9394-c50fdbc70c8d"), row.names = c(1L, 2L, 
  4L, 6L, 8L, 10L, 11L, 16L, 20L, 22L, 23L, 30L, 32L, 37L, 38L, 
  43L, 45L, 46L, 47L, 49L, 50L, 53L, 62L, 64L, 65L, 67L, 68L, 69L, 
  70L, 71L, 73L, 74L, 76L, 77L, 79L, 80L, 81L), class = "data.frame")
  </code></pre>