Kashif Ali
Kashif Ali

Reputation: 197

How to slice certain rows of a data frame and make them in to columns of a new data frame?

After melting a large array and chopping off unnecessary columns I came up with following data frame structure (with 30 observations):

         value
1   1.00000000
2   1.00000000
3   1.00000000
4   0.00000000
5   0.00000000
6  -0.53871342
7  -1.72755740
8   0.24669587
9  -2.09057167
10 -0.51257170
11  1.71501643
12  0.71394349
13 -0.32088042
14 -0.47352206
15 -1.27711506
16 -0.63105474
17  0.23659050
18  0.46110755
19  0.35898478
20 -0.49026141
21  1.02293578
22 -1.03308196
23  0.21874966
24  0.37300023
25  1.77300259
26 -1.78736439
27 -0.13571158
28 -0.36234039
29  0.01959764
30 -0.09142165

Now, I want to make a new data frame from above 30 values into 6 variables each of 5 observations i.e., 1-5 values will form one variable, 6-10 will make second variable and ... 26-30 be the 6th variable.

How can I do that in R?

Upvotes: 1

Views: 480

Answers (1)

akrun
akrun

Reputation: 887108

You can try

as.data.frame(matrix(df1$value, ncol=6))
#  V1         V2         V3         V4         V5          V6
#1  1 -0.5387134  1.7150164 -0.6310547  1.0229358 -1.78736439
#2  1 -1.7275574  0.7139435  0.2365905 -1.0330820 -0.13571158
#3  1  0.2466959 -0.3208804  0.4611075  0.2187497 -0.36234039
#4  0 -2.0905717 -0.4735221  0.3589848  0.3730002  0.01959764
#5  0 -0.5125717 -1.2771151 -0.4902614  1.7730026 -0.09142165

Upvotes: 1

Related Questions