add one column below another in a data.frame in R

You could also do :

df2 <- data.frame(a = c(df[,"a"], df[,"b"]))

We can use unlist to create a vector and then wrap it with data.frame to create a new dataset.

d1 <- data.frame(a=unlist(df, use.names = FALSE))
d1
#    a
#1   2
#2   5
#3  10
#4   3
#5   8
#6   6
#7   9
#8   7
#9   1
#10  4
#11  9
#12  8
#13  1
#14  7
#15  4
#16 10
#17  5
#18  6
#19  3
#20  2

You can select a and bind it to b in the following way:

df <- data.frame(a=sample(1:10), b=sample(1:10))
df %>% 
  select(a) %>% 
  bind_rows(
    df %>% 
      transmute(a = b)
  )

You can use pivot_longer() from tidyr package.

library(dplyr)
set.seed(1)
df <- data.frame(a=sample(1:10), b=sample(1:10))
pivot_longer(df, a:b)


# A tibble: 20 x 2
   name  value
   <chr> <int>
 1 a         3
 2 b         3
 3 a         4
 4 b         2
 5 a         5
 6 b         6
 7 a         7
 8 b        10
 9 a         2
10 b         5
11 a         8
12 b         7
13 a         9
14 b         8
15 a         6
16 b         4
17 a        10
18 b         1
19 a         1
20 b         9

Tags:

R