我有一个向量,围绕3000
元素很长。我使用提取了向量中的特定点which(...)
。现在,我想拥有-120 before
这一点和+120 after
这一点。
我的清单如下所示:
> testList$Date
[1] "01.01.2002" "02.01.2002" "03.01.2002" "04.01.2002" "07.01.2002"
[6] "08.01.2002" "09.01.2002" "10.01.2002" "11.01.2002" "14.01.2002"
[11] "15.01.2002" "16.01.2002" "17.01.2002" "18.01.2002" "21.01.2002"
[16] "22.01.2002" "23.01.2002" "24.01.2002" "25.01.2002" "28.01.2002"
[21] "29.01.2002" "30.01.2002" "31.01.2002" "01.02.2002" "04.02.2002"
[26] "05.02.2002" "06.02.2002" "07.02.2002" "08.02.2002" "11.02.2002"
[31] "12.02.2002" "13.02.2002" "14.02.2002" "15.02.2002" "18.02.2002"ect....
我可以做一个for-loop
遍历列表并将其保存为子列表。但是,我认为这不是非常有效。如何在R中实现呢?
感谢您的回答!
更新使用时,lapply
我得到:
> 120BeforeSublist <- lapply(event, function(x) c(x-120, x))
> (120BeforeSublist)
[[1]]
[1] 1875 1995
但是我想保存子列表-120 before and +120 after
。
原则上描述您的问题和一般解决方案:
x <- 1:20
pos <- which( x == 10 )
end <- 2
len <- 5
x_bef <- x[ ( pos - len - end ) : ( pos - end ) ]
x_aft <- x[ (pos + end ) : ( pos + len + end ) ]
x_bef
[1] 3 4 5 6 7 8
x_aft
[1] 12 13 14 15 16 17
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句