Max Beikirch
Max Beikirch

Reputation: 2143

R iterate over consecutive pairs in a list

suppose I have the following vector:

v1 = c(1,2,3,4)

I need to iterate over this vector in a pairwise fashion, like (1,2), (2,3), (3,4). For python, there is a solution to this problem here: Iterate over all pairs of consecutive items in a list. Can a similar solution be achieved in R?

Upvotes: 4

Views: 727

Answers (4)

ThomasIsCoding
ThomasIsCoding

Reputation: 101628

A base R option using embed + asplit

> asplit(embed(v1,2)[,2:1],1)
[[1]]
[1] 1 2

[[2]]
[1] 2 3

[[3]]
[1] 3 4

Upvotes: 1

G. Grothendieck
G. Grothendieck

Reputation: 269654

These solutions all easily generalize to windows of greater than 2 by replacing 2 with some other number.

1) rollapply Replace toString with whatever function you want to use.

library(zoo)
rollapply(v1, 2, toString)
## [1] "1, 2" "2, 3" "3, 4"

1a) or create a 2 column matrix and then iterate over that in a second step:

library(zoo)
m <- rollapply(v1, 2, c)
apply(m, 1, toString)
## [1] "1, 2" "2, 3" "3, 4"

2) embed or use embed. This does not use any packages.

e <- embed(v1, 2)[, 2:1]
apply(e, 1, toString)
## [1] "1, 2" "2, 3" "3, 4"

Upvotes: 3

jay.sf
jay.sf

Reputation: 72929

Just cbind the shifted vector.

cbind(v1[-length(v1)], v1[-1])
#      [,1] [,2]
# [1,]    1    2
# [2,]    2    3
# [3,]    3    4

R is vectorized, no need for iteration.

Upvotes: 4

akrun
akrun

Reputation: 887163

We can remove the first and last elements and concatenate in Map

Map(c,  v1[-length(v1)], v1[-1])
#[[1]]
#[1] 1 2

#[[2]]
#[1] 2 3

#[[3]]
#[1] 3 4

Or rbind and use asplit

asplit(rbind(v1[-length(v1)], v1[-1]), 2)

Upvotes: 8

Related Questions