将IRanges列表作为列包含在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的列中。这是可能的操作吗?

回复
  • [呛人心] 回复

    这是一个很好的问题,我之前在其他数据框(如对象)中已经看到过,但是我认为上述方法不起作用,因为在data.frames中进行了一些检查。

    一种选择是使用小标题:

    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