簡體   English   中英

R:用另一個列表替換一個列表

[英]R: Subsetting a list by another list

這是一個非常瑣碎的問題,所以我希望有人可以在這里幫助我。

我有兩個列表,我想根據另一個列表中的值對一個列表進行子集化。

> 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
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`

暫無
暫無

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

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