<p>You can create a vector consisting of values that you would like to keep (or discard):</p>

<pre><code>to_keep &lt;- paste0("chr", seq(1,22))

&gt; to_keep
 [1] "chr1"  "chr2"  "chr3"  "chr4"  "chr5"  "chr6"  "chr7"  "chr8"  "chr9"  "chr10" "chr11"
[12] "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22"

your_df[your_df$Chromosome %in% to_keep,]
</code></pre>

<p>The <code>data.table</code> way, which should be faster for large data, would be:</p>

<pre><code>your_df[Chromosome %in% to_keep]
</code></pre>

<p>I don't know what your goal is but you might want to keep chromosomes X and Y as well.</p>

CC BY-SA 4.0
2020-01-19T15:06:35.853Z
2020-01-19T15:06:35.853Z
haci
<p>I have a big data frame</p>

<pre><code>&gt; dim(anno_maf)
[1] 449892    132
&gt; 
</code></pre>

<p>In chromosome column I have chr 1 to 22 and another contigs like</p>

<pre><code>&gt; unique(anno_maf$Chromosome)
 [1] "chr1"                  "chr6"                 
 [3] "chr16"                 "chr17"                
 [5] "chr20"                 "chr2"                 
 [7] "chr3"                  "chr4"                 
 [9] "chr14"                 "chr19"                
[11] "chr5"                  "chr10"                
[13] "chr9"                  "chr12"                
[15] "chr13"                 "chr11"                
[17] "chr22"                 "chr7"                 
[19] "chr15"                 "chr8"                 
[21] "chr18"                 "chr21"                
[23] "chrX"                  "chrY"                 
[25] "chr4_gl000194_random"  "chr17_gl000205_random"
[27] "chrUn_gl000241"        "hs37d5"               
[29] "chrUn_gl000219"        "chrUn_gl000234"       
[31] "chr1_gl000191_random"  "chrUn_gl000211"       
[33] "chrUn_gl000224"        "chrUn_gl000225"       
[35] "chr17_gl000203_random" "chrUn_gl000212"       
[37] "chrUn_gl000243"        "chrUn_gl000214"       
[39] "chrM"                  "chr1_gl000192_random" 
[41] "chr7_gl000195_random"  "chrUn_gl000232"       
[43] "chr4_gl000193_random"  "chr19_gl000208_random"
[45] "chrUn_gl000226"        "chrUn_gl000218"       
[47] "chr9_gl000199_random"  "chrUn_gl000217"       
[49] "chrUn_gl000229"        "chrUn_gl000216"       
[51] "chrUn_gl000231"        "chr9_gl000198_random" 
[53] "chr17_gl000204_random" "chrUn_gl000220"       
[55] "chrUn_gl000235"        "chr11_gl000202_random"
[57] "chrUn_gl000222"        "chrUn_gl000240"       
[59] "chrUn_gl000233"        "chrUn_gl000230"       
[61] "chrUn_gl000213"        "chrUn_gl000238"       
[63] "chr19_gl000209_random" "chrUn_gl000237"       
[65] "#CHROM" 

  How I can only keep rows having chr1 to chr22 and removing the rest?
</code></pre>

<r>
Removing rows containing some strings
# Removing rows containing some strings

(Asked by: username_0 on 2020-01-19)

I have a big data frame
> dim(anno_maf)
[1] 449892    132
> 

In chromosome column I have chr 1 to 22 and another contigs like
> unique(anno_maf$Chromosome)
 [1] "chr1"                  "chr6"                 
 [3] "chr16"                 "chr17"                
 [5] "chr20"                 "chr2"                 
 [7] "chr3"                  "chr4"                 
 [9] "chr14"                 "chr19"                
[11] "chr5"                  "chr10"                
[13] "chr9"                  "chr12"                
[15] "chr13"                 "chr11"                
[17] "chr22"                 "chr7"                 
[19] "chr15"                 "chr8"                 
[21] "chr18"                 "chr21"                
[23] "chrX"                  "chrY"                 
[25] "chr4_gl000194_random"  "chr17_gl000205_random"
[27] "chrUn_gl000241"        "hs37d5"               
[29] "chrUn_gl000219"        "chrUn_gl000234"       
[31] "chr1_gl000191_random"  "chrUn_gl000211"       
[33] "chrUn_gl000224"        "chrUn_gl000225"       
[35] "chr17_gl000203_random" "chrUn_gl000212"       
[37] "chrUn_gl000243"        "chrUn_gl000214"       
[39] "chrM"                  "chr1_gl000192_random" 
[41] "chr7_gl000195_random"  "chrUn_gl000232"       
[43] "chr4_gl000193_random"  "chr19_gl000208_random"
[45] "chrUn_gl000226"        "chrUn_gl000218"       
[47] "chr9_gl000199_random"  "chrUn_gl000217"       
[49] "chrUn_gl000229"        "chrUn_gl000216"       
[51] "chrUn_gl000231"        "chr9_gl000198_random" 
[53] "chr17_gl000204_random" "chrUn_gl000220"       
[55] "chrUn_gl000235"        "chr11_gl000202_random"
[57] "chrUn_gl000222"        "chrUn_gl000240"       
[59] "chrUn_gl000233"        "chrUn_gl000230"       
[61] "chrUn_gl000213"        "chrUn_gl000238"       
[63] "chr19_gl000209_random" "chrUn_gl000237"       
[65] "#CHROM" 

  How I can only keep rows having chr1 to chr22 and removing the rest?

**Tags:** r

---

## 1 answers

### Answer by username_1 on 2020-01-19. Score: 1 | Accepted answer

You can create a vector consisting of values that you would like to keep (or discard):
to_keep <- paste0("chr", seq(1,22))

> to_keep
 [1] "chr1"  "chr2"  "chr3"  "chr4"  "chr5"  "chr6"  "chr7"  "chr8"  "chr9"  "chr10" "chr11"
[12] "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22"

your_df[your_df$Chromosome %in% to_keep,]

The data.table way, which should be faster for large data, would be:
your_df[Chromosome %in% to_keep]

I don't know what your goal is but you might want to keep chromosomes X and Y as well.


---

