This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Recoding columns independently of one another in R

Hi,

I'm working with a data frame of genotypes produced from multiallelic indels, an example would be

Column 1    Column 2
0/3         0/0
0/2         0/1
1/1         0/2
0/0         0/2

The possible genotypes depends on how many alternative alleles there are for that indel (I have this data)

I need to recode the 0/0 into a single digit format so for example if an indel has 1 reference allele and 2 alternative alleles the possible genotypes would be 0/0 0/1 0/2 1/1 1/2 2/2.

I tried recoding them in bulk but as each column has a different number of alleles it doesn't work correctly, so I need to code the columns independently of one another.

Any help would be great thanks

r

I don't understand the question:

I need to recode the 0/0 into a single digit format so for example if an indel has 1 reference allele and 2 alternative alleles the possible genotypes would be 0/0 0/1 0/2 1/1 1/2 2/2.

0/0, 0/1, 0/2 are not single digits. Can you make an complete example of how the table you posted should look like after the recoding?

Original table

Column 1    Column 2
0/3         0/0
0/2         0/1
1/1         0/2
0/0         0/2

Recoded table

Column 1    Column 2
1           1
3           4
5           2
1           3

So in the example of 2 alternatives

  • 0/0 = 1
  • 0/1 = 2
  • 0/2 = 3
  • 1/1 = 4
  • 1/2 = 5
  • 2/2 = 6

1 answer

The stringr R package has a str_replace_all() function that will allow in a single lane to substitute 0/0 by 1, 0/1 by 2, etc.

library(stringr)
str_replace_all(your_data.frame, "0/0", "1")

gsub() does it as well, but I find stringr to be sometime easier to use for novel users

Log in to answer this question.