Q:

Merging Columns within the same dataset with R

I have a data set like the following:
a  b  c  d  e  f  g  h  i
1  2  3  4  5  6  7  8  9

I would like to keep 3 columns and add the other 6 columns under the first three columns like this:
a  b  c
1  2  3
4  5  6
7  8  9

A:

We can do
n <- 3
do.call(rbind, lapply(split.default(df1, f = gl(
  n = n, k = n, length = ncol(df1)
)), setNames, nm = names(df1)[1:n]))
#  a b c
#1 1 2 3
#2 4 5 6
#3 7 8 9

Explaination
The first step is to split your data a long every three columns. We use split.default() in combination with gl() for this task. gl() defines the grouping.
gl(n = n, k = n, length = ncol(df1))
#[1] 1 1 1 2 2 2 3 3 3
#Levels: 1 2 3

The result of split.default is a list
split.default(df1, f = gl(n = n, k = n, length = ncol(df1)))
#$`1`
#  a b c
#1 1 2 3
#
#$`2`
#  d e f
#1 4 5 6
#
#$`3`
#  g h i
#1 7 8 9

In order to use rbind, the columns must have the same names. Hence, I use lapply() and setNames to rename all columns to be names(df1)[1:n], i.e. 
lapply(split.default(df1, f = gl(n = n, 
                                 k = n,
                                 length = ncol(df1))), 
       setNames, nm = names(df1)[1:n])

Finally we call do.call(rbind, ...) to combine the three datasets into one. 
data
df1 <- structure(list(a = 1L, b = 2L, c = 3L, d = 4L, e = 5L, f = 6L, 
    g = 7L, h = 8L, i = 9L), .Names = c("a", "b", "c", "d", "e", 
"f", "g", "h", "i"), class = "data.frame", row.names = c(NA, 
-1L))

