Recoding columns independently of one another in R
1
1
Entering edit mode
8.8 years ago
doommonk ▴ 10

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 • 2.0k views
ADD COMMENT
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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
ADD REPLY
0
Entering edit mode
8.8 years ago

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

ADD COMMENT

Login before adding your answer.

Traffic: 2975 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6