Description Usage Arguments Details Value Note Author(s) See Also Examples
Reshape a data frame by reducing the multiple rows of repeated variables to a single row for each instance (usually a "case" or object) and stretching out the variables that are not repeated within each case.
1 | stretch_df(x,idvar,to.stretch,ordervar=NA,include.ordervar=TRUE)
|
x |
A data frame. |
idvar |
A variable that identifies instances (cases or objects). |
to.stretch |
Which variables are to be stretched out in the single row. |
ordervar |
Variable that gives the order of the stretched variables. |
include.ordervar |
Include the ordering variable in the output. |
stretch_df takes a data frame in which at least some instances have multiple rows and reshapes it into a "wide" format with one row per instance. The variable passed as idvar distinguishes the instances, and will be the first column in the new data frame. All other variables in the data frame except those named in to.stretch and ordervar will follow idvar. The variables named in to.stretch will follow the variables that are not repeated in the initial data frame, along with the order variable if include.ordervar is TRUE.
The reshaped data frame.
stretch_df mostly does what other reshaping functions can do, but may be more easy to understand. It will stretch multiple variables, something that some reshaping functions will not do.
Jim Lemon
reshape
1 2 3 4 5 6 | # create a data frame with two repeated variables
longdf<-data.frame(ID=c(rep(111,3),rep(222,4),rep(333,6),rep(444,3)),
name=c(rep("Joe",3),rep("Bob",4),rep("Sue",6),rep("Bea",3)),
score1=sample(1:10,16,TRUE),score2=sample(0:100,16),
scoreorder=c(1,2,3,4,3,2,1,4,6,3,5,1,2,1,2,3))
stretch_df(longdf,"ID",c("score1","score2"),"scoreorder")
|
ID name score1_1 score1_2 score1_3 score1_4 score1_5 score1_6 score2_1
1 111 Joe 7 4 7 NA NA NA 42
2 222 Bob 6 10 10 2 NA NA 36
3 333 Sue 7 10 6 2 5 3 96
4 444 Bea 2 7 6 NA NA NA 44
score2_2 score2_3 score2_4 score2_5 score2_6 scoreorder_1 scoreorder_2
1 38 93 NA NA NA 1 2
2 33 62 9 NA NA 1 2
3 32 21 37 28 22 1 2
4 7 18 NA NA NA 1 2
scoreorder_3 scoreorder_4 scoreorder_5 scoreorder_6
1 3 NA NA NA
2 3 4 NA NA
3 3 4 5 6
4 3 NA NA NA
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.