功能无法使用print和gsub

时间:2017-10-28 03:48:04

标签: r

知道如何让这个功能起作用吗?我希望它在列中的20行打印“Seq is DNA,转换为RNA ..”或“Seq is RNA”,然后将gsub应用于空列,但它不起作用。我得到的是第一行的打印,而gsub不起作用。

     `$DNA.RNA = function(check.string){
      grep = grepl("[^ACGT]",seq.mat[1:20,2])
      DNAorRNA = ifelse(grep == "FALSE", print("Seq is DNA, converting to 
      RNA.."), print("Seq is RNA"))
      DNAorRNA = as.list(DNAorRNA)
      seq.mat[,3] = gsub("T", "U", seq.mat[,2])
      }`

没有seq.mat [,3]代码行,当我运行函数时,这将在rstudio控制台中打印

   `$[1] "Seq is DNA, converting to RNA.."
    [[1]]
    [1] "Seq is DNA, converting to RNA.."
    [[2]]
    [1] "Seq is DNA, converting to RNA.."
    [[3]]
    [1] "Seq is DNA, converting to RNA.."
    [[4]]
    [1] "Seq is DNA, converting to RNA.."
    [[5]]
    [1] "Seq is DNA, converting to RNA.."
    [[6]]
    [1] "Seq is DNA, converting to RNA.."
    [[7]]
    [1] "Seq is DNA, converting to RNA.."
    [[8]]
    [1] "Seq is DNA, converting to RNA.."
    [[9]]
    [1] "Seq is DNA, converting to RNA.."
    [[10]]
    [1] "Seq is DNA, converting to RNA.."
    [[11]]
    [1] "Seq is DNA, converting to RNA.."
    [[12]]
    [1] "Seq is DNA, converting to RNA.."
    [[13]]
    [1] "Seq is DNA, converting to RNA.."
    [[14]]
    [1] "Seq is DNA, converting to RNA.."  
    [[15]]
    [1] "Seq is DNA, converting to RNA.."
    [[16]]
    [1] "Seq is DNA, converting to RNA.."
    [[17]]
    [1] "Seq is DNA, converting to RNA.."
    [[18]]
    [1] "Seq is DNA, converting to RNA.."
    [[19]]
    [1] "Seq is DNA, converting to RNA.."
    [[20]]
    [1] "Seq is DNA, converting to RNA.."
    `

seq.mat [,3]行我想用来将数据从一列转换为另一列,如果打印的行表示特定的引用并用U替换所有的T。

1 个答案:

答案 0 :(得分:0)

根据我的理解,你想要:

  1. 检查'ACGT'seq.mat[, 2]中元素的一部分。
  2. 如果是这样,那么你正在处理你想要转换RNA的DNA,对吗?
  3. 然后,您想要给seq.math第3列有效的RNA值。
  4. 您通过用'T'替换DNA元素中的每个字母'U',从DNA转换为RNA,对吧?
  5. 这是基于你的问题,我对DNA,RNA或类似的东西一无所知。

    # Part 1:
    dna_check <- grepl("ACGT", seq.math[, 2])
    
    # Part 2-3:
    seq.math[!dna_check, 3] <- seq.math[!dna_check, 2]
    seq.math[dna_check, 3]  <- gsub("T", "U", seq.mat[dna_check, 2])
    

    我的猜测是,这将非常快,打印消息只会减慢速度。如果你真的想在中间打印一些消息,你应该使用for - 循环左右,并在每次迭代结束时打印一条消息。

    基于您的问题的附注:您的问题中有几件事令人困惑,多余甚至错误。我想我会指出它们,这样你就可以向它们学习。首先,定义一个以check.string作为参数的函数,但是你从不在函数中使用它。其次,您要在列表中保存print电话。这样做是没有充分理由的(据我所知)。如果要在列表中保存邮件,只需使用my_list <- ifelse(check, "message1", "message2")之类的引号。第三,正如Akrun在他的评论中指出的那样,定义与R函数同名的对象是一个坏主意。另外,为什么将DNAorRNA转换为列表?最后,我认为在自定义函数中使用return语句是个好主意。尽管它本身并不是必需的,但它可以帮助您组织函数在运行时实际输出的内容。