Adding multiple integer ranges of values from a column in the ifelse statement in R

一世执手 提交于 2020-01-14 02:38:06

问题


I am dealing with genomic data and I have columns on nucleotide position and its conservation score (in a dataframe). I have data regarding which range of nucleotide positions are introns and which are exons. I want to create a third column and be able to specify which regions are introns (as "INTRON") and which are exons (as "EXON").

As an example suppose in nucleotide positions 1-70000, I want to specify 10000-10200, 17800-21000, 43000-54000 as introns and remaining as exons in another column (hypothetical data). Is there a way of specifying multiple ranges of values from a column in the ifelse function, as that would more or less solve my problem. Is there a better way of doing it ?


回答1:


Assuming you have data frame like that:

 d <- data.frame(position=round(runif(100, 1, 70000)))

You can combine logical operators:

 d$status <- ifelse(( d$position >= 1000 & d$position <= 10200) | (d$position >= 17800 & d$position <= 21000) | (d$position >= 43000 & d$position <= 54000), 'INTRON', 'EXON')

or you can use nested ifelse:

d$status <- ifelse(d$position >= 1000 & d$position <= 10200, 'INTRON', felse(d$position >= 17800 & d$position <= 21000, 'INTRON', ifelse(d$position >= 43000 & d$position <= 54000, 'INTRON', 'EXON')))


来源:https://stackoverflow.com/questions/18685519/adding-multiple-integer-ranges-of-values-from-a-column-in-the-ifelse-statement-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!