简体   繁体   中英

How to combine and convert multiple rows into one column?

How to combine and convert multiple rows into one column? I have been searching but can't find a suitable solution for my question. eg: please see Example df , how do I convert every rows into column and combine then them into one column, so the new df should have 25 rows with 1 column. Just like Expected Outcome Thanks.

Example df

   Size   H-Size L-Size A-Size S-Size
1: 1.1111 1.1111 1.1111 1.1111 11465
2: 2.2222 2.2222 2.2222 2.2222 11419 
3: 3.3333 3.3333 3.3333 3.3333 11534
4: 4.4444 4.4444 4.4444 4.4444 11154
5: 5.5555 5.5555 5.5555 5.5555 11640

Expected Outcome

   V1
1: 1.1111 
2: 1.1111 
3: 1.1111 
4: 1.1111 
5: 11465
6: 2.2222 
7: 2.2222 
8: 2.2222 
9: 2.2222 
10: 11419 
11: 3.3333 
12: 3.3333 
13: 3.3333 
14: 3.3333 
15: 11534
16: 4.4444 
17: 4.4444 
18: 4.4444 
19: 4.4444 
20: 11154
21: 5.5555 
22: 5.5555 
23: 5.5555 
24: 5.5555 
25: 11640

The input dataset seems to be data.table . We transpose ( t ) the dataset, convert it to a vector and create a new data.table

library(data.table)
d1 <- data.table(v1 = c(t(df)))
head(d1, 4)
#       v1
#1: 1.1111
#2: 1.1111
#3: 1.1111
#4: 1.1111

Or it can be also done by

df[, .(v1 = t(.SD))]

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