如何在其他两个col上创建txt文件中的新col

时间:2018-01-18 11:06:41

标签: r file loops

我们说我有这个信息的txt文件

   exons affected_exon
    24  22
    37  7
    14  7
    33  3

So what I want to do is to create a new col called splicing like this:
exons affected_exon splicing
        24  22   midle
        37  7    middle 
        14  14   last
        33  1    first

所以对于拼接我想要跟随:

if affected_exon = 1 -> complete with first
if affected exon = exons -> complete with last
if affected exon < exons (and > 1) -> complete with middle 

目前我在R中有这个代码,但是没有用,我无法弄清楚原因:

out <- read.delim("out.txt", sep = "\t", header = T)
  scans <- out$exons
  exon <- out$affected_exon
  cluster <- out$group 
  for (i in out ){
    if (scans == exon){
    cluster <- c("last")
    }
    if (exon == 1){
    cluster <- c("first")
    } 
    if (exon < scans & exon > 1){
    cluster <- c("middle")
  }
}

1 个答案:

答案 0 :(得分:1)

我建议将其作为data.table

阅读
data_set = data.table::fread("out.txt", header = TRUE)

然后你可以为所有人设置:

data_set[, cluster := "middle"]

并将其更改为最后一个:

data_set[affected_exon == 1, cluster := "first"]
data_set[affected_exon == exons, cluster := "last"]

你可以写下来:

data.table::fwrite(data_set, "textfile.txt", sep = "\t")

希望有所帮助