-
Notifications
You must be signed in to change notification settings - Fork 29
/
matconvert.ado
83 lines (61 loc) · 2.16 KB
/
matconvert.ado
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*** DO NOT EDIT THIS LINE -----------------------------------------------------
Version:
Title: matconvert
Description: __matconvert__ belongs to {help rcall} package. It takes the
name of a Stata matrix and returns a code for constructing the same matrix in R.
for more information visit [rcall homepage](http://www.haghish.com/packages/Rcall.php).
----------------------------------------------------- DO NOT EDIT THIS LINE ***/
/***
Example(s)
=================
convert Stata matrix to R code
{bf:. matrix A = (1,2\3,4)}
{bf:. matconvert A}
matrix(c(1,2,3,4), nrow=2, byrow = TRUE)
Author
======
__E. F. Haghish__
Center for Medical Biometry and Medical Informatics
University of Freiburg, Germany
_and_
Department of Mathematics and Computer Science
University of Southern Denmark
[http://www.haghish.com/packages/Rcall.php](http://www.haghish.com/packages/Rcall.php)
Package Updates on [Twitter](http://www.twitter.com/Haghish)
- - -
This help file was dynamically produced by {help markdoc:MarkDoc Literate Programming package}
***/
*cap prog drop matconvert
program matconvert, rclass
//Get number of rows
local row = rowsof(`0')
local col = colsof(`0')
//Get the names
local rownames : rownames `0'
local colnames : colnames `0'
local data
//extract the scalars
forval i = 1/`row' {
forval j = 1/`col' {
//avoid the first comma
if !missing("`data'") local data "`data',"
*di `0'[`i',`j']
*di `"`0'[`i',`j']"'
local data : display "`data'" `0'[`i',`j']
}
}
if !missing("`rownames'") | !missing("`colnames'") {
local code : display "matrix(c(`data'), nrow=`row', byrow = TRUE, " ///
"dimnames=list(unlist(strsplit("`"""' "`rownames'" "" `"""' ", split=" `"""' " " `"""' ")), " ///
"unlist(strsplit("`"""' "`colnames'" "" `"""' ", split=" `"""' " " `"""' ")) ))"
}
else {
local code "matrix(c(`data'), nrow=`row', byrow = TRUE)"
}
display as txt `"{p}`code'"'
return local `0' "`code'"
end
// Create help file
// ============================================
* markdoc matconvert.ado, export(sthlp) replace