简体   繁体   English

R:如何根据第一个字符 0|0 vs 1|0 vs 0|1 vs 1|1 重新编码值

[英]R: How to recode values based on first characters 0|0 vs 1|0 vs 0|1 vs 1|1

Thank you in advance for the help.预先感谢您的帮助。

I am trying to recode a genetic database that contains genotypes coded in VCF format.我正在尝试重新编码一个包含以 VCF 格式编码的基因型的基因数据库。 For context, the VCF format is coded in this format: '0|0:0,0:0:1,0,0'.对于上下文,VCF 格式以这种格式编码:'0|0:0,0:0:1,0,0'。 The main thing I am interested in is the first two(/three if including the |) characters: 0|0 :0,0:0:1,0,0.我最感兴趣的是前两个(/三个如果包括|)字符: 0|0 :0,0:0:1,0,0。 If these are 0|0, it means that the person has two dominant alleles.如果这些为 0|0,则意味着该人具有两个显性等位基因。 IF these are 1|1, two recessive alleles.如果这些是 1|1,则两个隐性等位基因。 1|0 and 0|1 are a mix of the two. 1|0 和 0|1 是两者的混合。

I am working on a data frame called "gg" that contains approx 120 columns (one for each SNP) and 1500 rows (one for each subject in the study).我正在研究一个名为“gg”的数据框,它包含大约 120 列(每个 SNP 一个)和 1500 行(研究中的每个主题一个)。

I am trying to recode the SNP from its current format to a more easily analysable format:我正在尝试将 SNP 从其当前格式重新编码为更易于分析的格式:

  • 0|0 = two dominant alleles - recode as 0 0|0 = 两个显性等位基因 - 重新编码为 0
  • 0|1 or 1|0 = mix of one dominant one recessive - recode as 1 0|1 或 1|0 = 一个显性和一个隐性的混合 - 重新编码为 1
  • 1|1= two recessive - recode as 2 1|1= 两个隐性 - 重新编码为 2

I have attempted several approaches.我尝试了几种方法。 The latest thing I have attempted has got close-ish.我最近尝试的事情已经接近了。 I tried the following:我尝试了以下方法:

gg[grep("0|0", gg)] <- "0"

Weirdly this makes all the values for the WHOLE database 0's.奇怪的是,这使得整个数据库的所有值都为 0。 I think this is because it is interpreting the 0|0 as 'if the value contains a zero or a zero, recode as zero' (and all values contain at least one zero).我认为这是因为它将 0|0 解释为“如果值包含零或零,则重新编码为零”(并且所有值都至少包含一个零)。

What I want to convey is to recode as 1 if the value starts with the EXACT characters 0|0, recode as 1 if it starts with the EXACT characters of 0|1 or 1|0, recode as 2 if it starts with the EXACT character of 1|1我想传达的是,如果值以 EXACT 字符 0|0 开头,则重新编码为 1,如果它以 0|1 或 1|0 的 EXACT 字符开头,则重新编码为 1,如果它以 EXACT 开头,则重新编码为 2 1|1 的字符

Try the code below试试下面的代码

colSums(list2DF(strsplit(substr(gsub("\\|","",gg),1,2),""))=="1")

which gives这使

0 1 1 2

Dummy Data虚拟数据

gg <- c('0|0:0,0:0:1,0,0','10:0,0:0:1,0,0','0|1:0,0:0:1,0,0','11:0,0:0:1,0,0')

A slightly modified option is稍作修改的选项是

rowSums(read.csv(text = sub("^(\\d)\\|?(\\d).*", "\\1,\\2", gg), 
         header = FALSE) == 1)
#[1] 0 1 1 2

data数据

gg <- c('0|0:0,0:0:1,0,0','10:0,0:0:1,0,0','0|1:0,0:0:1,0,0','11:0,0:0:1,0,0')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM