【发布时间】:2022-01-19 18:01:56
【问题描述】:
我有几个序列,我想将它们分成一系列相邻的数字。这些序列嵌套在一个个体列表中,因此包含相邻数字的窗口大小因个体而异。以下是一些示例数据:
#The sequences of three individuals
sequences <- list(c(1,2,3,5,6), c(2,3,4,5,6), c(1,3,4,6,7))
#The window size that contains the adjacent numbers
#for the first individual, 2 adjacent numbers should be bonded together and for the second, 3 should be bonded, etc.
windowsize <- list(2,3,4)
#The breakdown of the adjacent numbers should look like:
[[1]]
[[1]][[1]]
[1] 1 2
[[1]][[2]]
[1] 2 3
[[1]][[3]]
[1] 3 5
[[1]][[4]]
[1] 5 6
[[2]]
[[2]][[1]]
[1] 2 3 4
[[2]][[2]]
[1] 3 4 5
[[2]][[3]]
[1] 4 5 6
[[3]]
[[3]][[1]]
[1] 1 3 4 6
[[3]][[2]]
[1] 3 4 6 7
我有一个比这大得多的数据集,所以我想也许编写一个函数将是实现这一目标的方法?谢谢!
【问题讨论】:
标签: r list sequence data-manipulation data-cleaning