% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cards.R
\name{cards}
\alias{cards}
\title{Convert a string to matrix\cr
 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{
\item{a}{string which contains the body of the matrix with \\nr.}

\item{rnameroot}{root of the row names when header=0}

\item{cnameroot}{root of the column names when header=0}

\item{header}{= 1 if the string has row/col names}

\item{missing}{a string which indicates missing values: "NA" or "."}

\item{nummat}{= 1 to force the result to be a numeric matrix. \cr
This may cause an error!}

\item{debug}{= 1 to dump intermediate result}
}
\value{
a matrix or data.frame
}
\description{
Convert a string to matrix\cr
 For those who miss the luxury of using spaces as delimitters.
}
\details{
Use space ( blank characters ) or a comma as delimiters. \cr
Two or more commas indicate the existence of  missing values in between.\cr
The last comma indicates one element is missing at the end.\cr
Blank lines will be neglected.

\preformatted{
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:
\preformatted{
     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. \cr

 When the string contains both row and column names
 as in a3 or a4 above, header=1 must be specified. \cr
 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. \cr
 Otherwise, as a4 above, C0 will be treated as the first column name
 of the resulting matrix. \cr
  \cr
 The row/col names cannot have commas in it. (for csv compatibility) \cr
 When the string does not have row and column names, characters specified
 in rnameroot or cnameroot will be used to generate the names. \cr
 Character elements of the matrix cannot have blanks in them. \cr
 The quotes will be treated as characters. \cr

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

\cr
 To input from the clip board, \cr
 copy the body of the matrix with row and column names and use \cr
   \code{ read.table(file("clipboard"),header=1) }
\cr
  \code{ write.table( string-such-as-a1-etc-above, "clipboard",sep=" ")}
   \cr
  may be used to improve this module.
}
\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)

}
