rep_n_stack: Replicate and stack columns of a data frame

Description Usage Arguments Details Value Note Author(s) See Also Examples

Description

Reshape a data frame by stacking two or more columns into one and adding a factor, while replicating the remaining columns and stacking them to match the number of rows

Usage

1
 rep_n_stack(data,to.stack,stack.names=NULL)

Arguments

data

A data frame.

to.stack

Which columns are to be stacked together (see Details).

stack.names

Names for the new factor and stacked column.

Details

rep_n_stack takes two or more specified columns in a data frame and "stacks" them into a single column. It also creates a new factor composed of the replicated names of the columns that identifies from which column each value came. The remaining columns in the data frame are replicated to match the new number of rows.

If to.stack is a matrix of names or column numbers, rep_n_stack will stack each row into two new columns, allowing multiple related sets of values to be stacked in one operation.

A matrix or data frame of values can now be stacked so that the values can be displayed by a function like barNest in the plotrix package.

Value

The reshaped data frame.

Note

rep_n_stack only does what other reshaping functions can do, but may be more easy to understand.

Author(s)

Jim Lemon

See Also

reshape

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 wide.data<-data.frame(ID=1:10,Glup=sample(c("Montic","Subtic"),10,TRUE),
  Flimit1=runif(10,1,2),Flimit2=runif(10,1.5,2.5),Flimit3=runif(10,1.2,3),
  Glimit1=rnorm(10,mean=5),Glimit2=rnorm(10,mean=4),Glimit3=rnorm(10,mean=4.5))
 # first just stack one set of related measures
 rep_n_stack(wide.data[,1:5],to.stack=c("Flimit1","Flimit2","Flimit3"))
 # now stack two sets of related measures and pass names for the stacks
 rep_n_stack(wide.data,to.stack=matrix(3:8,nrow=2,byrow=TRUE),
  stack.names=c("Limit_F","Value_F","Limit_G","Value_G"))
 # finally stack a matrix of means into a single column with the
 # row and column names becoming "factor" variables
 meanmat<-matrix(runif(16,10,20),nrow=4)
 rownames(meanmat)<-c("Plunderers","Storers","Refusers","Jokers")
 colnames(meanmat)<-c("Week1","Week2","Week3","Week4")
 rep_n_stack(meanmat,to.stack=1:4,
  stack.names=c("Returns","Occasion","Strategy"))

Example output

   ID   Glup  group1   value1
1   1 Montic Flimit1 1.965013
2   2 Montic Flimit1 1.360218
3   3 Montic Flimit1 1.793731
4   4 Subtic Flimit1 1.942254
5   5 Subtic Flimit1 1.532713
6   6 Montic Flimit1 1.987145
7   7 Subtic Flimit1 1.960388
8   8 Montic Flimit1 1.298934
9   9 Montic Flimit1 1.953499
10 10 Montic Flimit1 1.515163
11  1 Montic Flimit2 1.880199
12  2 Montic Flimit2 1.743127
13  3 Montic Flimit2 1.800832
14  4 Subtic Flimit2 2.300527
15  5 Subtic Flimit2 1.545277
16  6 Montic Flimit2 1.599481
17  7 Subtic Flimit2 1.701701
18  8 Montic Flimit2 1.724925
19  9 Montic Flimit2 2.433570
20 10 Montic Flimit2 1.943867
21  1 Montic Flimit3 1.574765
22  2 Montic Flimit3 2.497191
23  3 Montic Flimit3 1.242745
24  4 Subtic Flimit3 2.270473
25  5 Subtic Flimit3 2.358930
26  6 Montic Flimit3 2.123425
27  7 Subtic Flimit3 2.084166
28  8 Montic Flimit3 1.842197
29  9 Montic Flimit3 1.966751
30 10 Montic Flimit3 2.206896
   ID   Glup Limit_F  Value_F Limit_G  Value_G
1   1 Montic Flimit1 1.965013 Glimit1 5.827955
2   2 Montic Flimit1 1.360218 Glimit1 5.199204
3   3 Montic Flimit1 1.793731 Glimit1 5.061475
4   4 Subtic Flimit1 1.942254 Glimit1 4.178426
5   5 Subtic Flimit1 1.532713 Glimit1 5.617247
6   6 Montic Flimit1 1.987145 Glimit1 6.505409
7   7 Subtic Flimit1 1.960388 Glimit1 5.980527
8   8 Montic Flimit1 1.298934 Glimit1 5.543885
9   9 Montic Flimit1 1.953499 Glimit1 4.286680
10 10 Montic Flimit1 1.515163 Glimit1 4.610692
11  1 Montic Flimit2 1.880199 Glimit2 3.058859
12  2 Montic Flimit2 1.743127 Glimit2 2.913772
13  3 Montic Flimit2 1.800832 Glimit2 5.922268
14  4 Subtic Flimit2 2.300527 Glimit2 3.116194
15  5 Subtic Flimit2 1.545277 Glimit2 3.452200
16  6 Montic Flimit2 1.599481 Glimit2 3.981941
17  7 Subtic Flimit2 1.701701 Glimit2 2.900887
18  8 Montic Flimit2 1.724925 Glimit2 3.927479
19  9 Montic Flimit2 2.433570 Glimit2 4.744267
20 10 Montic Flimit2 1.943867 Glimit2 3.870520
21  1 Montic Flimit3 1.574765 Glimit3 4.019536
22  2 Montic Flimit3 2.497191 Glimit3 5.083032
23  3 Montic Flimit3 1.242745 Glimit3 4.773916
24  4 Subtic Flimit3 2.270473 Glimit3 5.607141
25  5 Subtic Flimit3 2.358930 Glimit3 3.545231
26  6 Montic Flimit3 2.123425 Glimit3 4.391134
27  7 Subtic Flimit3 2.084166 Glimit3 3.535808
28  8 Montic Flimit3 1.842197 Glimit3 4.629514
29  9 Montic Flimit3 1.966751 Glimit3 4.824797
30 10 Montic Flimit3 2.206896 Glimit3 2.657362
    Returns Occasion   Strategy
1  13.75603    Week1 Plunderers
2  11.55153    Week2 Plunderers
3  10.46311    Week3 Plunderers
4  15.44216    Week4 Plunderers
5  16.87220    Week1    Storers
6  15.30316    Week2    Storers
7  10.75845    Week3    Storers
8  11.17856    Week4    Storers
9  15.69008    Week1   Refusers
10 19.31256    Week2   Refusers
11 11.72718    Week3   Refusers
12 18.89821    Week4   Refusers
13 18.77454    Week1     Jokers
14 12.47009    Week2     Jokers
15 11.80552    Week3     Jokers
16 18.99444    Week4     Jokers

prettyR documentation built on May 2, 2019, 2:16 a.m.

Related to rep_n_stack in prettyR...