Description Usage Arguments Details Value Author(s) Examples
transforms long format data.frame into a matrix format data.frame
1 2 3 |
dat |
data.frame with columns "row" , "col" , "val" |
sort |
sort rows and columns of matrix |
triangle |
if not |
force.diagonal |
a diagonal is forced into matrix even if no diagonal elements are in |
exclude.diagonal |
the diagonal is excluded if possible |
long2matrix |
if |
WARNING: This function seems to be buggy. Do not use it or use it with care.
long2matrix = TRUE |
data.frame in matrix format |
long2matrix = FALSE |
data.frame in long format |
Martin Hecht
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | d1 <- data.frame (
"row" = c ( "v1" , "v2" , "v2" , "v3" , "v1" , "v3" ) ,
"col" = c ( "v1" , "v3" , "v2" , "v1" , "v2" , "v3" ) ,
"val" = c ( 1 , 5 , 4 , 3 , 2 , 6 ) , stringsAsFactors = FALSE )
# unsorted matrix
long2matrix ( dat = d1 , sort = FALSE )
# sorted by default
long2matrix ( dat = d1 )
# extract upper triangle of symmetric matrix
long2matrix ( dat = d1 , triangle = "upper" )
# exclude diagonal elements
long2matrix ( dat = d1 , triangle = "upper" , exclude.diagonal = TRUE )
# if full matrix ("both" triangles) is requested, the diagonal cannot be excluded, option is ignored
long2matrix ( dat = d1 , triangle = "both" , exclude.diagonal = TRUE )
# no diagonal elements are specified
d2 <- data.frame (
"row" = c ( "v2" , "v1" , "v1" ) ,
"col" = c ( "v3" , "v3" , "v2" ) ,
"val" = c ( 5 , 3 , 2 ) , stringsAsFactors = FALSE )
long2matrix ( dat = d2 )
# diagonal is set (with NAs)
long2matrix ( dat = d2 , triangle = "upper" , force.diagonal = TRUE )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.