我有幾個序列,我想將它們分成一系列相鄰的數字。這些序列嵌套在一個個體串列中,這樣包含相鄰數字的視窗的大小因人而異。以下是一些示例資料:
#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
我有一個比這大得多的資料集,所以我想也許撰寫一個函式將是實作這一目標的方法?謝謝!
uj5u.com熱心網友回復:
我們可以使用Mapwith embedfrom base R- 回圈遍歷 'sequences', 'windowsize' in 的相應元素Map,創建一個矩陣,embed其中dimension指定為y'windowsize' 中的元素 ( ) 并用于asplit按行拆分 ( MARGIN = 1)
Map(function(x, y) asplit(embed(x, y)[, y:1], 1), sequences, windowsize)
-輸出
[[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
如果我們想要 a matrix,只需洗掉asplit
Map(function(x, y) embed(x, y)[, y:1], sequences, windowsize)
[1]]
[,1] [,2]
[1,] 1 2
[2,] 2 3
[3,] 3 5
[4,] 5 6
[[2]]
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 3 4 5
[3,] 4 5 6
[[3]]
[,1] [,2] [,3] [,4]
[1,] 1 3 4 6
[2,] 3 4 6 7
uj5u.com熱心網友回復:
我們真的不需要在這里創建串列串列,因為子串列都是矩形的。建議改為創建矩陣串列。我們使用了問題中的序列和視窗大小,但因為視窗大小可以由一個可能更有意義的數字向量表示。如果您仍然想要串列,請改用注釋掉的 f。
library(zoo)
# split2 <- function(x) split(x, 1:nrow(x))
# f <- function(x, w) split2(rollapply(x, w, c))
f <- function(x, w) rollapply(x, w, c)
Map(f, sequences, windowsize)
給予:
[[1]]
[,1] [,2]
[1,] 1 2
[2,] 2 3
[3,] 3 5
[4,] 5 6
[[2]]
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 3 4 5
[3,] 4 5 6
[[3]]
[,1] [,2] [,3] [,4]
[1,] 1 3 4 6
[2,] 3 4 6 7
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383113.html
