Julio Diaz
Julio Diaz

Reputation: 9437

Using rbind to merge all columns in data frame

I have the following data frame

> mydata
X1  X2  X3  X4  X5  X6
1   1   1   1   1   1
1   0   0   0   0   1
1   1   0   0   0   0
1   0   1   1   1   0
1   1   0   1   1   1
1   0   1   0   1   0
1   1   0   1   1   0

I would like to merge all the columns from this dataframe. I can accomplish this by specifying all the columns like this.

rbind(mydata$X1,ydata$X2,ydata$X3,ydata$X4,ydata$X5,ydata$X6)

What is the best way to merge all the columns in a data frame without having to specify each column? I tried

bind(mydata)

but it doesn't work.

Upvotes: 1

Views: 6712

Answers (1)

yeedle
yeedle

Reputation: 5008

tidyr's gather does exactly that:

tidyr::gather(mydata)

If you then want to get rid of the "key" column, dplyr::select(mydata, value) will do the trick (or simply, mydata$value if you don't want to keep it as a dataframe)

Upvotes: 2

Related Questions