How to combine and convert multiple rows into one column?

How to combine and convert multiple rows into one column? I searched, but cannot find a suitable solution for my question. for example: see Example dfhow to convert all rows to a column and combine them into one column, so the new one dfshould have 25 rows with 1 column. Just like Expected Outcome Thank you.

Df example

   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 Result

   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
+4
source share
1 answer

The input dataset looks like data.table. We transfer ( t) the data set, convert it to vectorand create a new onedata.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 also be done with

df[, .(v1 = t(.SD))]
+3
source

Source: https://habr.com/ru/post/1678765/


All Articles