简体   繁体   中英

How do you stack two columns in r?

I have been trying this all night. Spent 6 hours trying to figure out how you take all the values from one column and append them to the end of another column. Or even make a new column c to contain all values from column a and column b? I have tried rbind, I have tried melt, I have tried gather. All of these give me error messages.

here is a little example 
a b
1 3     
2 4
I want to do this:
c
1
2
3
4

these columns are over five thousand rows each

Create a new dataframe unlisting the columns.

df1 <- data.frame(c = unlist(df))

Or if you have other columns you can select only the columns that you want.

df1 <- data.frame(c = c(df$a, df$b))

We can also do

df1 <- data.frame(c = do.call(c, df))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM