cards {lazy.tools}R Documentation

Convert a string to matrix
For those who miss the luxury of using spaces as delimitters.

Description

Convert a string to matrix
For those who miss the luxury of using spaces as delimitters.

Usage

cards(
  a,
  rnameroot = "r",
  cnameroot = "c",
  header = 0,
  missing = "NA",
  nummat = 0,
  debug = 0
)

Arguments

a

string which contains the body of the matrix with \nr.

rnameroot

root of the row names when header=0

cnameroot

root of the column names when header=0

header

= 1 if the string has row/col names

missing

a string which indicates missing values: "NA" or "."

nummat

= 1 to force the result to be a numeric matrix.
This may cause an error!

debug

= 1 to dump intermediate result

Details

Use space ( blank characters ) or a comma as delimiters.
Two or more commas indicate the existence of missing values in between.
The last comma indicates one element is missing at the end.
Blank lines will be neglected.

CARDS <- " v1 v2 v3
s1 11 12 13
s2 21 22 23
s3 31 32 33"
A <- cards( CARDS, header=1 )

A will be a 3 x 3 matrix with row and column names.

This module converts the following types of string:

     a1="         , a2="11 12, 13  ,  a3="C1 C2 C3  ,  a4="C0 C1 C2 C3
         11 12 13       21, 22  23     R1 11 12 13         R1 11 12 13
         21 22 23       31 32 33       R2 21 22 23         R2 21 22 23
         31 32 33       41,, 43"       R3 31 32 33         R3 31 32 33
         41 42 43                      R4 41 42 43"        R4 41 42 43"
        "

into a matrix or data.frame.

When the string contains both row and column names as in a3 or a4 above, header=1 must be specified.
If, as a3 above, the first line ending with \n has one less elements than the rest, it is assumed that both row and col names are given.
Otherwise, as a4 above, C0 will be treated as the first column name of the resulting matrix.

The row/col names cannot have commas in it. (for csv compatibility)
When the string does not have row and column names, characters specified in rnameroot or cnameroot will be used to generate the names.
Character elements of the matrix cannot have blanks in them.
The quotes will be treated as characters.


When a column contains both numeric and character string a data frame, not a matrix, will be created.
Scientific notation like 1e-9 will be mistaken as character.


To input from the clip board,
copy the body of the matrix with row and column names and use
read.table(file("clipboard"),header=1)
write.table( string-such-as-a1-etc-above, "clipboard",sep=" ")
may be used to improve this module.

Value

a matrix or data.frame

Examples

CARDS=" v1 v2 v3
s1 11 12 13
s2 21 22 23

s3 NA 32 33"
cards( CARDS, header=1 )

CARDS=" v1 v2 v3
s1, 11, 12, 13
s2, 21, 22, 23
s3,,    32, 33"
cards( CARDS, header=1 )

CARDS="v0 v1 v2 v3
s1 11 12 13
s2 21 22 23
s3 .  32 33"
cards( CARDS, header=1, missing="." )

CARDS="
 11 12 13
 21 22 23
 NA  32 33
 "
cards( CARDS  )

# result is a character matrix.
CARDS <- " cos(theta) -sin(theta)
           sin(theta) cos(theta)"
T <- cards(CARDS)


# result is a data frame.
CARDS <- "
num char num
11 aa 12
21 bb 22
"
df <- cards( CARDS, header=1 )
str(df)

CARDS <- "
     ncat      a         b1        b2
 r1  3.00e+00  1.25e+00 -2.06e+00 .
 r2  3.00e+00  1.36e+00 -1.44e-16 .
 r3  3.00e+00  1.40e+00  1.97e+00 .
 r4  3.00e+00  1.02e+00 -2.04e+00 .
 r5  3.00e+00  1.06e+00 -1.46e-16 .
 r6  3.00e+00  1.07e+00  1.98e+00 .
 r7  4.00e+00  1.10e+00 -2.13e+00  1.05e-15
 r8  4.00e+00  7.19e-01 -2.03e+00 -3.27e-16
 "
 Ad <- cards(CARDS, header=1, missing=".")
 Print("This is a character matrix.", Ad)

 Ad <- cards(CARDS, header=1, missing=".", nummat=1)
 Print("This is a numeric matrix.", Ad)


[Package lazy.tools version 0.1.4 ]