Skip to content
Prev 395372 / 398503 Next

How to Reformat a dataframe

You can also use the pivot_longer to do it:

library(tidyverse)

input <- structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
19.3120917, 113.12581575, 28.45843425, 114.400074, 143.0000925,
46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
:
:
:
 row.names = c(NA, -126L), class = "data.frame")
1  92.99254 34.99963 24.04101 43.01330 53.00914 62.01390 91.01036 88.99986
2  76.00243 22.00219 22.00219 25.00378 44.99547 60.99449 63.00499 92.99254
3  44.99547 22.99328 15.00793 15.99902 38.00121 44.00438 62.01390 79.99510
4  28.00536 19.00061 12.99743 12.99743 44.00438 49.01647 55.95410 69.00816
5 120.00681 35.99072 22.99328 47.99706 60.00341 62.01390 60.00341 66.00658
6  31.99804 23.95606 13.98852 15.99902 38.99230 54.99132 88.00877 89.99095
      ...9    ...10     ...11     ...12 row
1 54.00023 75.01134 111.99314  49.01647   1
2 68.01707 75.01134  82.99669  63.99608   2
3 60.99449 91.01036  84.01609  65.01549   3
4 82.99669 78.01292 135.01474  85.99827   4
5 66.99767 91.01036  88.99986  51.98974   5
6 79.00401 78.01292 113.52225 155.00644   6> > x <-
pivot_longer(input,names_to = "col",cols=1:12)> head(x, 20)# A tibble:
20 ? 3
     row col   value
   <int> <chr> <dbl> 1     1 ...1   93.0 2     1 ...2   35.0 3     1
...3   24.0 4     1 ...4   43.0 5     1 ...5   53.0 6     1 ...6
62.0 7     1 ...7   91.0 8     1 ...8   89.0 9     1 ...9   54.010
1 ...10  75.011     1 ...11 112. 12     1 ...12  49.013     2 ...1
76.014     2 ...2   22.015     2 ...3   22.016     2 ...4   25.017
2 ...5   45.018     2 ...6   61.019     2 ...7   63.020     2 ...8
93.0
Thanks

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*
On Fri, Oct 27, 2023 at 10:41?PM Paul Bernal <paulbernal07 at gmail.com> wrote: