How do I format roxygen2 return list similar to parameter list? - roxygen2

I've noticed that using \describe{} to create a list of returned objects does not have the same formatting as I see in documentation for packages I've downloaded from CRAN. How do I mimic their style? See the images below for the comparison. The code I used the generate the first is:
#' #return A list containing:\describe{
#' \item{pars}{A numeric vector of parameter estimates}
#' \item{std.errs}{A numeric vector of standard errors on parameters}
#' \item{cov.mat}{Parameter covariance matrix (excluding mean)}
#' }

I figured it out. You need to use the tabular environment, code text formatting, and additional lines. The below code works.
#' #return A list containing:\tabular{ll}{
#' \code{pars} \tab A numeric vector of parameter estimates \cr
#' \tab \cr
#' \code{std.errs} \tab A numeric vector of standard errors on parameters \cr
#' \tab \cr
#' \code{cov.mat} \tab Parameter covariance matrix (excluding mean) \cr
#' }

Related

Powershell number format

I am creating a script converting a csv file in an another format.
To do so, i need my numbers to have a fixed format to respect column size : 00000000000000000,00 (20 characters, 2 digits after comma)
I have tried to format the number with -f and the method $value.toString("#################.##") without success
Here is an example Input :
4000000
45817,43
400000
570425,02
15864155,69
1068635,69
128586256,9
8901900,04
29393,88
126858346,88
1190011,46
2358411,95
139594,82
13929,74
11516,85
55742,78
96722,57
21408,86
717,01
54930,49
391,13
2118,64
Any hints are welcome :)
Thank you !
tl;dr:
Use 0 instead of # in the format string:
PS> $value = 128586256.9; $value.ToString('00000000000000000000.00')
00000000000128586256.90
Note:
Alternatively, you could construct the format string as an expression:
$value.ToString('0' * 20 + '.00')
The resulting string reflects the current culture with respect to the decimal mark; e.g., with fr-FR (French) in effect, , rather than . would be used; you can pass a specific [cultureinfo] object as the second argument to control what culture is used for formatting; see the docs.
As in your question, I'm assuming that $value already contains a number, which implies that you've already converted the CSV column values - which are invariably strings - to numbers.
To convert a string culture-sensitively to a number, use [double]::Parse('1,2'), for instance (this method too has an overload that allows specifying what culture to use).
Caveat: By contrast, a PowerShell cast (e.g. [double] '1.2') is by design always culture-invariant and only recognizes . as the decimal mark, irrespective of the culture currently in effect.
zerocukor287 has provided the crucial pointer:
To unconditionally represent a digit in a formatted string and default to 0 in the absence of an available digit, use 0, the zero placeholder in a .NET custom numeric format string
By contrast, #, the digit placeholder, represents only digits actually present in the input number.
To illustrate the difference:
PS> (9.1).ToString('.##')
9.1 # only 1 decimal place available, nothing is output for the missing 2nd
PS> (9.1).ToString('.00')
9.10 # only 1 decimal place available, 0 is output for the missing 2nd
Since your input uses commas as decimal point, you can split on the comma and format the whole number and the decimal part separately.
Something like this:
$csv = #'
Item;Price
Item1;4000000
Item2;45817,43
Item3;400000
Item4;570425,02
Item5;15864155,69
Item6;1068635,69
Item7;128586256,9
Item8;8901900,04
Item9;29393,88
Item10;126858346,88
Item11;1190011,46
Item12;2358411,95
Item13;139594,82
Item14;13929,74
Item15;11516,85
Item16;55742,78
Item17;96722,57
Item18;21408,86
Item19;717,01
Item20;54930,49
Item21;391,13
Item22;2118,64
'# | ConvertFrom-Csv -Delimiter ';'
foreach ($item in $csv) {
$num,$dec = $item.Price -split ','
$item.Price = '{0:D20},{1:D2}' -f [int64]$num, [int]$dec
}
# show on screen
$csv
# output to (new) csv file
$csv | Export-Csv -Path 'D:\Test\formatted.csv' -Delimiter ';'
Output in screen:
Item Price
---- -----
Item1 00000000000004000000,00
Item2 00000000000000045817,43
Item3 00000000000000400000,00
Item4 00000000000000570425,02
Item5 00000000000015864155,69
Item6 00000000000001068635,69
Item7 00000000000128586256,09
Item8 00000000000008901900,04
Item9 00000000000000029393,88
Item10 00000000000126858346,88
Item11 00000000000001190011,46
Item12 00000000000002358411,95
Item13 00000000000000139594,82
Item14 00000000000000013929,74
Item15 00000000000000011516,85
Item16 00000000000000055742,78
Item17 00000000000000096722,57
Item18 00000000000000021408,86
Item19 00000000000000000717,01
Item20 00000000000000054930,49
Item21 00000000000000000391,13
Item22 00000000000000002118,64
I do things like this all the time, usually for generating computernames. That custom numeric format string reference will come in handy. If you want a literal period, you have to backslash it.
1..5 | % tostring 00000000000000000000.00
00000000000000000001.00
00000000000000000002.00
00000000000000000003.00
00000000000000000004.00
00000000000000000005.00
Adding commas to long numbers:
psdrive c | % free | % tostring '0,0' # or '#,#'
18,272,501,760
"Per mille" character ‰ :
.00354 | % tostring '#0.##‰'
3.54‰

Powershell: Convert unique string to unique int

Is there a method for converting unique strings to unique integers in PowerShell?
I'm using a PowerShell function as a service bus between two API's,
the first API produces unique codes e.g. HG44X10999 (varchars)- but the second API which will consume the first as input, will only accept integers. I only care about keeping them unique.
I have looked at $string.gethashcode() but this produces negative integers and also changes between builds. Get-hash | $string -encoding ASCII obviously outputs varchars too.
Other examples on SO are referring to converting a string of numeric characters to integers i.e. $string = 123 - but I can't find a way of quickly computing an int from a string of alphanumeric
The Fowler-Noll-Vo hash function seems well-suited for your purpose, as it can produce a 32-bit hash output.
Here's a simple implementation in PowerShell (the offset basis and initial prime is taken from the wikipedia reference table for 32-bit outputs):
function Get-FNVHash {
param(
[string]$InputString
)
# Initial prime and offset chosen for 32-bit output
# See https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
[uint32]$FNVPrime = 16777619
[uint32]$offset = 2166136261
# Convert string to byte array, may want to change based on input collation
$bytes = [System.Text.Encoding]::UTF8.GetBytes($InputString)
# Copy offset as initial hash value
[uint32]$hash = $offset
foreach($octet in $bytes)
{
# Apply XOR, multiply by prime and mod with max output size
$hash = $hash -bxor $octet
$hash = $hash * $FNVPrime % [System.Math]::Pow(2,32)
}
return $hash
}
Now you can repeatably produce distinct integers from the input strings:
PS C:\> Get-FNVHash HG44X10999
1174154724
If the target API only accepts positive signed 32-bit integers you can change the modulus to [System.Math]::Pow(2,31) (doubling the chance of collisions, to
approx. 1 in 4300 for 1000 distinct inputs)
For further insight into this simple approach, see this page on FNV and have a look at this article exploring short string hashing

Powershell generate a hexadecimal list

I am looking to generate a list of hex values using Powershell. For instance, [0..100] creates an array of numbers 0-100.
How would I go about creating an array of values from 000-FFF?
You can use following expression:
0..0xfff|% ToString X3
Where:
0 is zero.
.. is range operator.
0xfff is integer literal for 4095 in hexadecimal form.
| is pipe operator.
% is alias for ForEach-Object cmdlet.
ToString is name of method to call.
X3 is parameter for method. It is standard numeric format string that means, format as hexadecimal number with at least three digits.
So:
0..0xfff creates an array of numbers 0-4095.
| pass elements of array to next command.
% ToString X3 format each number as hexadecimal with at least three digits.

Understanding the fastaread function in matlab

I searched the documentation about the fastaread function, but I'm still kind of confused about the Sequence part.
So, suppose my file is stored in the file_path location, and fastaread(file_path) will return the data obtained.
fastaread returns two columns, one is with title header and the other one is with title sequence. Then, fastaread(file_path).sequence will return the sequence column? Does that mean that fastaread(file_path).sequence is a column vector?
Actually it's a row vector containing characters.
Example with data provided with Matlab:
p53nt = fastaread('p53nt.txt')
gives a structure with 2 fields: Header and Sequence.
p53nt.Header gives info about the actual sequence:
p53nt.Header
ans =
gi|8400737|ref|NM_000546.2| Homo sapiens tumor protein p53 (Li-Fraumeni syndrome) (TP53), mRNA
while p53nt.Sequence gives a character array of size 1xN:
S = p53nt.Sequence
S =
ACTTGTCATGGCGACTGTCCAGC... And so on
Typing whos S gives this:
Name Size Bytes Class Attributes
S 1x2629 5258 char
So if you want a column vector for some reason, use the colon operator:
S = S(:);
Hope its a bit clearer now!

stepwise regression: Undefined function ' stepwiselm' for input arguments of type 'cell'

I have one .txt file and I have converted it to first a table Ta(Ta=readtable('xxx.txt')) then an array Aa(Aa=table2array(Ta)), the .txt file contains 220 rows and 12 cols, but the table and the array only have 219 rows and 1 col. Where did I do wrong?
Then when I tried to do stepwise regression I got error message: Undefined function ' stepwiselm' for input arguments of type 'cell'.
My coad was: mdl=stepwiselm(Aa)
In the .txt file, the first raw are texts e.g. elevation, hight, yields etc. I though I could use these names to define Predictor variables and Response variable. But since these names are lost in Aa, how should I write code for stepwise regression?Thanks!
Try the following
delim = ' ';
nrhdr = 1;
A = importdata('A-100spreg2-raa06a.txt', delim, nrhdr);
A.data will be your data, A.textdata your header. A ".txt" does not contain columns, so you need to specify a delimiter (I assumed a space). You can then use your A.data in your stepwise function.
As you indicated you wanted column 10 as y, and I assume others as X, use
stepwise(A.data(:,1:9),A.data(:,10))
I wouldn't use the headers for anything other than creating labels in figures.