我有一个字符向量,它看起来像(dput输出):
c(" genotype[0]= rv props(rr,rv,vv)= 0.000 0.944 0.056 reads= 10 , variants= 7",
" genotype[1]= na ", " genotype[2]= rv props(rr,rv,vv)= 0.000 1.000 0.000 reads= 16 , variants= 8",
" genotype[3]= rv props(rr,rv,vv)= 0.001 0.999 0.000 reads= 13 , variants= 5",
" genotype[4]= vv props(rr,rv,vv)= 0.000 0.489 0.511 reads= 10 , variants= 8",
" genotype[5]= vv props(rr,rv,vv)= 0.000 0.051 0.949 reads= 10 , variants= 9",
" genotype[6]= rr props(rr,rv,vv)= 1.000 0.000 0.000 reads= 20 , variants= 0",
" genotype[7]= rv props(rr,rv,vv)= 0.000 1.000 0.000 reads= 15 , variants= 7",
" genotype[8]= rr props(rr,rv,vv)= 0.975 0.025 0.000 reads= 7 , variants= 0",
" genotype[9]= vv props(rr,rv,vv)= 0.000 0.001 0.999 reads= 17 , variants= 16"
)我想返回每个字符向量的概率三元组中的第二个值,即0.944、0.999等。但是,我还希望结果中的NULL或其他值指示以genotype[1]开头的行没有匹配。
我的代码如下所示:
regmatches(genotype_strings, regexpr("[[:digit:]].[[:digit:]]+ [[:digit:]].[[:digit:]]+ [[:digit:]].[[:digit:]]+", genotype_strings))虽然结果只有9个元素,而我希望有10个元素。
提前感谢!
发布于 2020-04-21 01:29:57
删除除数字、点和空格以外的所有字符。然后我们可以使用read.table来读取它。提取第三个字段。
read.table(text = gsub("[^0-9. ]", "", v), fill = TRUE)[, 3]
## [1] 0.944 NA 1.000 0.999 0.489 0.051 0.000 1.000 0.025 0.001https://stackoverflow.com/questions/61328229
复制相似问题