Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort and subset data in r

I am trying to sort a row based on the first letters of the cells and i am having hard time to write a code in r.

snp allele1 allele2
mmsoop A A
rs3122 C G
SNP1234 T C
rs3144 A A

The above is the example dataset to show how my dataset looks like and i want to subset the whole table based on snp row where the snp column starts with "rs" and "SNP"

Expected table:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

snp allele1 allele2
rs3122 C G
SNP1234 T C
rs3144 A A

Any help is appreciated!!

>Solution :

Alternatively,

df<- read.table(
  text= "
snp allele1 allele2
mmsoop  A   A
rs3122  C   G
SNP1234 T   C
rs3144  A   A",
  header=T
)

df[grep("^(SNP|rs)",df$snp),]

     snp allele1 allele2
2  rs3122       C       G
3 SNP1234       T       C
4  rs3144       A       A

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading