简体   繁体   English

使用开始和结束位置之间的值创建列表

[英]create list with values between begin and end positions

I have a text file with this format 我有一个这种格式的文本文件

begin    end   
1        10    
25       35
40       50
37       48
...      ...

I use these commands to make a list that contains all the values that are between the values from 'begin' and 'end' column 我使用这些命令创建一个列表,其中包含“begin”和“end”列之间值的所有值

x <- read.table("in.txt")

result <- vector("list",486)
      for(i in 1:486){
      result[[i]] <- c(x[i,1]:x[i,2])
      }
lapply(result, write, "out.txt", append=TRUE, ncolumns = 1) 

So as result I get a file with 1 column where all the values are on different lines. 因此,我得到一个包含1列的文件,其中所有值都在不同的行上。 Now I want to do something extra. 现在我想做一些额外的事情。

Instead of an input file with only a 'begin' and 'end' column, I have two extra columns, like this: 我只有两个额外的列,而不是只包含'begin'和'end'列的输入文件,如下所示:

begin    end    A    B
1        10     x    0
25       35     x    1
40       50     x    2
37       48     y    0

I want now that the values of these other columns also appear in my output, so that I get something like this 我现在想要这些其他列的值也出现在我的输出中,所以我得到这样的东西

position    A    B
1           X    0
2           X    0
3           X    0
...
10          X    0
...
40          X    2
41          X    2
...
37          Y    0        

How can I change my function, so that the output looks like this? 如何更改我的功能,以便输出看起来像这样?

Here's a data.table solution: 这是一个data.table解决方案:

require(data.table)
DT <- data.table(DF, key=c("A", "B"))
DT[, list(pos = seq(begin, end, by=1)),by=key(DT)]

Here's a base answer: 这是一个基本答案:

lapply(1:nrow(x), function(u) cbind(position=x$begin[u]:x$end[u], x[u,3:4]))

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM