0

I have a list of elements in R that I've combined horizontally using the following function:

df <- do.call(cbind.data.frame, list)

Instead of combining each column horizontally, I would like to stack the columns vertically. For example, the current solution after using my code looks like this:

Date        C1  C2  C3  C4  C5
1/1/2017    36  55  50  89  47
1/2/2017    22  53  22  41  23
1/3/2017    24  43  96  100 42
1/4/2017    17  75  45  88  54
1/5/2017    43  83  83  66  71
1/6/2017    5   6   1   86  26
1/7/2017    47  92  58  46  78
1/8/2017    4   55  95  34  71
1/9/2017    20  7   3   80  52
1/10/2017   55  65  26  85  38
1/11/2017   78  63  20  49  24
1/12/2017   70  40  58  88  11
1/13/2017   40  63  75  61  38
1/14/2017   14  45  30  93  72
1/15/2017   2   64  88  68  81
1/16/2017   56  8   24  44  4
1/17/2017   94  47  48  31  77
1/18/2017   79  96  15  85  89
1/19/2017   95  33  45  39  48

Whereas I would like it to look like this:

Date    Price   C#
1/1/2017    36  C1
1/2/2017    22  C1
1/3/2017    24  C1
1/4/2017    17  C1
1/5/2017    43  C1
1/6/2017    5   C1
1/7/2017    47  C1
1/8/2017    4   C1
1/9/2017    20  C1
1/10/2017   55  C1
1/11/2017   78  C1
1/12/2017   70  C1
1/13/2017   40  C1
1/14/2017   14  C1
1/15/2017   2   C1
1/16/2017   56  C1
1/17/2017   94  C1
1/18/2017   79  C1
1/19/2017   95  C1
1/1/2017    55  C2
1/2/2017    53  C2
1/3/2017    43  C2
1/4/2017    75  C2
1/5/2017    83  C2
1/6/2017    6   C2
1/7/2017    92  C2
1/8/2017    55  C2
1/9/2017    7   C2
1/10/2017   65  C2
1/11/2017   63  C2
1/12/2017   40  C2
1/13/2017   63  C2
1/14/2017   45  C2
1/15/2017   64  C2
1/16/2017   8   C2
1/17/2017   47  C2
1/18/2017   96  C2
1/19/2017   33  C2
1/1/2017    50  C2
1/2/2017    22  C3
1/3/2017    96  C3
1/4/2017    45  C3
1/5/2017    83  C3
1/6/2017    1   C3
1/7/2017    58  C3
1/8/2017    95  C3
1/9/2017    3   C3
1/10/2017   26  C3
1/11/2017   20  C3
1/12/2017   58  C3
1/13/2017   75  C3
1/14/2017   30  C3
1/15/2017   88  C3
1/16/2017   24  C3
1/17/2017   48  C3
1/18/2017   15  C3
1/19/2017   45  C3
1/1/2017    89  C3
1/2/2017    41  C3
1/3/2017    100 C4
1/4/2017    88  C4
1/5/2017    66  C4
1/6/2017    86  C4
1/7/2017    46  C4
1/8/2017    34  C4
1/9/2017    80  C4
1/10/2017   85  C4
1/11/2017   49  C4
1/12/2017   88  C4
1/13/2017   61  C4
1/14/2017   93  C4
1/15/2017   68  C4
1/16/2017   44  C4
1/17/2017   31  C4
1/18/2017   85  C4
1/19/2017   39  C4
1/1/2017    47  C4
1/2/2017    23  C4
1/3/2017    42  C4
1/4/2017    54  C5
1/5/2017    71  C5
1/6/2017    26  C5
1/7/2017    78  C5
1/8/2017    71  C5
1/9/2017    52  C5
1/10/2017   38  C5
1/11/2017   24  C5
1/12/2017   11  C5
1/13/2017   38  C5
1/14/2017   72  C5
1/15/2017   81  C5
1/16/2017   4   C5
1/17/2017   77  C5
1/18/2017   89  C5
1/19/2017   48  C5

How can I modify the c.bind function to combine the lists vertically instead of horizontally?

neilfws
  • 32,751
  • 5
  • 50
  • 63
user6883405
  • 393
  • 3
  • 14

1 Answers1

0

Use cbind function and stack functions on the df using previous code:

cbind(mydf[1:1], stack(lapply(mydf[2:5], as.character)))
user6883405
  • 393
  • 3
  • 14