R:按另一个列表子集列表

时间:2014-08-20 07:47:37

标签: r list subset

这是一个非常微不足道的问题,所以我希望有人可以帮助我。

我有两个列表,我希望根据另一个列表中的值对一个列表进行子集化。

> head(islands)
RleViewsList of length 6
names(6): chr1 chr2 chr3 chr4 chr5 chr6

> head(islands$chr1)
Views on a 249250621-length Rle subject

views:
    start   end width
[1] 10001 10104   104 [ 1  2  3  3  4  4  5  6  7  7  8  8  9 10 11 11 12 ...]
[2] 10109 10145    37 [ 1  2  2  3  3  4  5  6  6  7  7  8  9 10 10 11 11 ...]
[3] 10149 10176    28 [1 1 2 3 4 4 5 5 5 6 7 7 7 7 7 7 7 7 7 7 7 5 4 4 4 ...]
[4] 10178 10229    52 [ 1  1  2  3  4  4  5  5  6  7  8  8  9  9 10 11 12 ...]
[5] 10256 10286    31 [1 2 2 3 3 4 5 6 6 7 7 7 8 9 9 9 9 9 8 7 7 7 7 7 5 ...]
[6] 10332 10388    57 [ 1  1  1  2  3  3  3  3  3  3  3  3  3  3  3  3  3 ...]

> names(islandsums)
 [1] "chr1"  "chr2"  "chr3"  "chr4"  "chr5"  "chr6"  "chr7"  "chr8"  "chr9"
[10] "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18"
[19] "chr19" "chr20" "chr21" "chr22" "chrM"  "chrX"  "chrY"
> head(islandsums$chr1)
[1] 1198  259  140  472  176  298


> length(islandsums)
[1] 25
> length(islandsums$chr1)
[1] 288625
> length(islands)
[1] 25
> length(islands$chr1)
[1] 288625

如果我在一个列表项上手动执行,一切都按照我的预期运行:

> head(islands$chr1[islandsums$chr1>1000])
Views on a 249250621-length Rle subject

views:
     start    end width
[1]  10001  10104   104 [ 1  2  3  3  4  4  5  6  7  7  8  8  9 10 11 11 ...]
[2]  50482  50514    33 [ 3 14 17 28 29 39 40 49 51 59 60 64 65 66 66 66 ...]
[3]  74555  74633    79 [  1   3   3  11  14  26  42  56  82 130 159 176 ...]
[4]  74908  74957    50 [76 76 76 76 76 76 76 76 76 76 76 76 77 77 77 77 ...]
[5] 109573 109615    43 [  1   1   1   4  15  18  29  32  43  46  57  60 ...]
[6] 121455 121529    75 [ 1  1  1  1  1  4  4  4  4  4  4  5  5  6 11 11 ...]

但是如果我尝试使用lapply将其应用到列表中,它就不起作用。

> head(lapply(islands, function(x) islands$x[islandsums$x>1000]))
$chr1
NULL

$chr2
NULL

$chr3
NULL

$chr4
NULL

$chr5
NULL

$chr6
NULL

这也没有,尽管它给出了不同的结果。

> head(lapply(islands, function(x) x[islandsums$x>1000]))
$chr1
Views on a 249250621-length Rle subject

views: NONE

$chr2
Views on a 243199373-length Rle subject

views: NONE

$chr3
Views on a 198022430-length Rle subject

views: NONE

$chr4
Views on a 191154276-length Rle subject

views: NONE

$chr5
Views on a 180915260-length Rle subject

views: NONE

$chr6
Views on a 171115067-length Rle subject

views: NONE

1 个答案:

答案 0 :(得分:1)

foo1 <- list(chr1= 1:25, chr2 = 5:15)
foo2 <- list(chr1= 7:31, chr2= 12:22)
 lapply(seq_along(foo1), function(i) foo1[[i]][foo2[[i]]>9 & foo2[[i]] <14])
 #[[1]]
 #[1] 4 5 6 7

 #[[2]]
#[1] 5 6

或者

  Map(function(x,y) x[y>9 & y <14], foo1, foo2)
  #    $chr1
  #[1] 4 5 6 7

  #$chr2
  #[1] 5 6

您的代码

  lapply(foo1, function(x) x) #gives the values of list elements
 #$chr1
 #[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

 #$chr2
 #  [1]  5  6  7  8  9 10 11 12 13 14 15

 lapply(foo1, function(x) foo2$x) @which is not the index for corresponding list elements in foo2
 #$chr1
 #NULL

 #$chr2
 #NULL

但是,

 lapply(seq_along(foo1), function(i) i ) gives the index of corresponding list elements
 #[[1]]
 #[1] 1

 #[[2]]
 #[1] 2

 lapply(seq_along(foo1), function(i) foo2[[i]] ) #gives the values of each list element in `foo2`