Question: Does anyone know a package/function that can do the following:

Assume I have a data frame:

```
1  2  3  4 
5  6  7  8
9  10 11 12
13 14 15 16

```

I want to transform it to:

```
1  2  3  4  5  6  7  8  9  10 11 12
5  6  7  8  9  10 11 12 13 14 15 16

```

Thanks

Answer: 
There are some nice functions in the `base` package that can help you get this done.

```
> d <- read.table(header = FALSE, text = "1  2  3  4 
  5  6  7  8
  9  10 11 12
  13 14 15 16")

> d
##   V1 V2 V3 V4
## 1  1  2  3  4
## 2  5  6  7  8
## 3  9 10 11 12
## 4 13 14 15 16

> do.call(rbind, lapply(seq(nrow(d)-1, nrow(d)), function(i){
      cbind(d[i-2,], d[i-1,], d[i,])
      }))
##   V1 V2 V3 V4 V1 V2 V3 V4 V1 V2 V3 V4
## 1  1  2  3  4  5  6  7  8  9 10 11 12
## 2  5  6  7  8  9 10 11 12 13 14 15 16

```

---

**ADD** 

More generally, we can take subsets of three consecutive rows and bind them together with the following. Assume the original data is `x`

```
> seqs <- lapply(seq(nrow(x)-2), function(i) seq(i, i+2))
> XX <- lapply(seq(seqs), function(i) x[seqs[[i]],])
> data.frame(t(sapply(XX, t)))

```
Not sure if this is exactly what you're looking for, but a possibly more simplistic method:

```
# Recreate the matrix
data=t(matrix(1:16,ncol=4))

#         [,1] [,2] [,3] [,4]
#    [1,]    1    2    3    4
#    [2,]    5    6    7    8
#    [3,]    9   10   11   12
#    [4,]   13   14   15   16

# Transform the matrix
newdata=matrix(t(data), nrow=2, byrow=T)

#           [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#      [1,]    1    2    3    4    5    6    7    8
#      [2,]    9   10   11   12   13   14   15   16

```

This doesn't return the exact transformation you gave, but maybe it's a more simple starting point. Could you describe the pattern you're looking for more explicitly? That might help generalize the method to any *m* x *n* matrix.