Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

split data frame with recurring column names

$
0
0

I have this big data containing a compilation of several data frames with common column names. To illustrate, it looks a little something like this:

df <- data_frame(A = c(runif(3), "A", runif(4), "A", runif(5)),
                 B = c(runif(3), "B", runif(4), "B", runif(5)),
                 C = c(runif(3), "C", runif(4), "C", runif(5)),
                 D = c(runif(3), "D", runif(4), "D", runif(5)))

How do I create a data frame for every new row that contains the original column names, such that I will get a result that somehow looks like this:

df1:
A     B     C     D     id
0.668 0.411 0.553 0.477 1
0.794 0.821 0.530 0.732 1
0.108 0.647 0.789 0.693 1

df2:
A     B     C     D     id
0.724 0.783 0.023 0.478 2
0.861 0.099 0.407 0.332 2
0.438 0.316 0.913 0.651 2
0.245 0.519 0.294 0.258 2
0.070 0.662 0.459 0.479 2
0.766 0.839 0.892 0.961 2

df3:
A     B     C     D     id
0.084 0.347 0.864 0.435 3
0.875 0.334 0.390 0.713 3
0.339 0.476 0.777 0.400 3
0.084 0.347 0.864 0.435 3

Thanks!


Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>