Question: Recoding columns independently of one another in R
1
gravatar for doommonk
5.0 years ago by
doommonk10
United Kingdom
doommonk10 wrote:

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

recoding R • 1.3k views
ADD COMMENTlink modified 5.0 years ago by Antonio R. Franco4.5k • written 5.0 years ago by doommonk10

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 REPLYlink written 5.0 years ago by Giovanni M Dall'Olio27k
0
gravatar for doommonk
5.0 years ago by
doommonk10
United Kingdom
doommonk10 wrote:

Original table

Column 1 Column 2
0/0 0/0
0/2 1/1
1/2 0/1
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 COMMENTlink written 5.0 years ago by doommonk10
0
gravatar for Antonio R. Franco
5.0 years ago by
Spain. Universidad de Córdoba
Antonio R. Franco4.5k wrote:

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 COMMENTlink modified 5.0 years ago • written 5.0 years ago by Antonio R. Franco4.5k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 926 users visited in the last hour