buhtz buhtz - 6 days ago 6
R Question

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

Not sure if I used the correct english words in the subject to describe what I need.

See this example

df <- data.frame(a=sample(1:10), b=sample(1:10))
df
a b
1 2 9
2 5 8
3 10 1
4 3 7
5 8 4
6 6 10
7 9 5
8 7 6
9 1 3
10 4 2


I want to take
b
column at put it below
a
as 10 new rows.

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


I found examples with
rbind
but couldn't find out how to use it in my situation.

Answer

You could also do :

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