如何在R中的列匹配模式中转换值

时间:2015-10-14 05:02:54

标签: r

我有这个数据框mydf。列nucleotide可以包含'A','T','G','C'字母。如果strand列为' - ',我想将字母A更改为T,C更改为G,G更改为C,将T更改为A.我该怎么做?

  mydf<- structure(list(seqnames = structure(c(1L, 1L, 1L, 1L), .Label = c("chr1", 
    "chr2", "chr3", "chr4", "chr5", "chr6", "chr7", "chr8", "chr9", 
    "chr10", "chr11", "chr12", "chr13", "chr14", "chr15", "chr16", 
    "chr17", "chr18", "chr19", "chr20", "chr21", "chr22", "chrX", 
    "chrY", "chrM"), class = "factor"), pos = c(115258748, 115258748, 
    115258748, 115258748), strand = structure(c(1L, 2L, 1L, 2L), .Label = c("+", 
    "-", "*"), class = "factor"), nucleotide = structure(c(2L, 2L, 
    2L, 2L), .Label = c("A", "C", "G", "T", "N", "=", "-"), class = "factor")), .Names = c("seqnames", 
    "pos", "strand", "nucleotide"), row.names = c(NA, 4L), class = "data.frame")

结果

 seqnames       pos strand nucleotide
1     chr1 115258748      +          C
2     chr1 115258748      -          G
3     chr1 115258748      +          C
4     chr1 115258748      -          G

1 个答案:

答案 0 :(得分:16)

对于一对一字符翻译,您可以使用chartr()

within(mydf, {
  nucleotide[strand == "-"] <- chartr("ACGT", "TGCA", nucleotide[strand == "-"])
})
#   seqnames       pos strand nucleotide
# 1     chr1 115258748      +          C
# 2     chr1 115258748      -          G
# 3     chr1 115258748      +          C
# 4     chr1 115258748      -          G

请注意,我在此处使用within()来避免写入mydf$四次并保存以免更改原始数据。您也可以编写以下内容,但请记住,您将更改原始数据。

mydf$nucleotide[mydf$strand == "-"] <- 
    with(mydf, chartr("ACGT", "TGCA", nucleotide[strand == "-"]))