long2matrix: long2matrix

Description Usage Arguments Details Value Author(s) Examples

Description

transforms long format data.frame into a matrix format data.frame

Usage

1
2
3
long2matrix ( dat , sort = TRUE , triangle = NULL ,
				force.diagonal = FALSE , exclude.diagonal = FALSE ,
				long2matrix = TRUE )

Arguments

dat

data.frame with columns "row" , "col" , "val"

sort

sort rows and columns of matrix

triangle

if not NULL a symmetric matrix will be constructed available options are "upper" , "lower" , "both"

force.diagonal

a diagonal is forced into matrix even if no diagonal elements are in dat

exclude.diagonal

the diagonal is excluded if possible

long2matrix

if FALSE dat is not transformed

Details

WARNING: This function seems to be buggy. Do not use it or use it with care.

Value

long2matrix = TRUE

data.frame in matrix format

long2matrix = FALSE

data.frame in long format

Author(s)

Martin Hecht

Examples

 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 )

eatRest documentation built on May 2, 2019, 6:25 p.m.

Related to long2matrix in eatRest...