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:
| 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