簡體   English   中英

在 data.frame 中包含 IRanges 列表作為列

[英]Include list of IRanges as column in a data.frame

我有一些數據結構有點像這樣:

x01 <- c("94633X94644Y95423X96130", "124240X124494Y124571X124714", "135654X135660Y136226X136786")

我最終通過以下步驟將其用作 IRanges 對象:

x02 <- sapply(x01,
              function(x) do.call(rbind,
                                  strsplit(strsplit(x,
                                                    split = "Y",
                                                    fixed = TRUE)[[1]],
                                           split = "X",
                                           fixed = TRUE)),
              simplify = FALSE,
              USE.NAMES = FALSE)

x03 <- sapply(x02,
              function(x) IRanges(start = as.integer(x[, 1L]),
                                  end = as.integer(x[, 2L])),
              simplify = FALSE,
              USE.NAMES = FALSE)

> x03
[[1]]
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]     94633     94644        12
  [2]     95423     96130       708

[[2]]
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]    124240    124494       255
  [2]    124571    124714       144

[[3]]
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]    135654    135660         7
  [2]    136226    136786       561

現在我希望能夠將 x03 存儲為 data.frame 中的一列,其中包含一些簡單的相關信息,例如:

> x04 <- data.frame("col1" = 1:3,
                    "col2" = x01,
                    "col3" = x03)

這毫不奇怪地告訴我,我有不同數量的行,但是,我覺得我已經看到 JSON 導入到 R 中模仿了我想要的那種結構,其中一個參差不齊的列表位於 data.frame 的列中。 這是一種可能的操作嗎?

這是一個很好的問題,我之前在其他數據幀(如對象)中見過它,但我認為上述方法不起作用,因為只要存在可用於矩陣或 IRanges 的 as.data.frame,它就會弄亂尺寸而不嵌入它(我可能錯了)。

一種選擇是使用小標題:

x04 = tibble::tibble(x01=x01,x02=x02,x03=x03)
# A tibble: 3 x 3
  a                           b                 c        
  <chr>                       <list>            <list>   
1 94633X94644Y95423X96130     <chr[,2] [2 x 2]> <IRanges>
2 124240X124494Y124571X124714 <chr[,2] [2 x 2]> <IRanges>
3 135654X135660Y136226X136786 <chr[,2] [2 x 2]> <IRanges>

x04$x03
[[1]]
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]     94633     94644        12
  [2]     95423     96130       708

[[2]]
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]    124240    124494       255
  [2]    124571    124714       144

[[3]]
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]    135654    135660         7
  [2]    136226    136786       561

另外一個選項:

library(S4Vectors)
DataFrame(x01=x01,x02=List(x02),x03=IRangesList(x03))
                          x01                             x02
                  <character>                          <List>
1     94633X94644Y95423X96130     94633:94644,95423:96130,...
2 124240X124494Y124571X124714 124240:124494,124571:124714,...
3 135654X135660Y136226X136786 135654:135660,136226:136786,...
                          x03
                <IRangesList>
1     94633-94644,95423-96130
2 124240-124494,124571-124714
3 135654-135660,136226-136786

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM