Stopping a large number of zeros being printed (not scientific notation) - r

What I'm trying to achieve is to have all printed numbers display at maximum 7 digits. Here are examples of what I want printed:
0.000000 (versus the actual number which is 0.000000000029481.....)
0.299180 (versus the actual number which is 0.299180291884922.....)
I've had success with the latter types of numbers by using options(scipen=99999) and options(digits=6). However, the former example will always print a huge number of zeros followed by five non-zero digits. How do I stop this from occurring and achieve my desired result? I also do not want scientific notation.
I want this to apply to ALL printed numbers in EVERY context. For example if I have some matrix, call it A, and I print this matrix, I want every element to just be 6-7 digits. I want this to be automatic for every print in every context; just like using options(digits=6) and options(scipen=99999) makes it automatic for every context.

You can define a new print method for the type you wish to print. For example, if all your numbers are doubles, you can create
print.double=function(x){sprintf("%.6f", x)}
Now, when you print a double (or a vector of doubles), the function print.double() will be called instead of print.default().
You may have to create similar functions print.integer(), print.complex(), etc., depending on the types you need to print.
To return to the default print method, simply delete the function print.double().

Are all your numbers < 1? You could try a simple sprintf( "%.6f", x ). Otherwise you could try wrapping things to sprintf based on the number of digits; check ?sprintf for other details.

Related

Combine IsNumeric and Right()

Trying to return field [doc] that have no letters. Results are all over the place.
SELECT Right([doc],4) AS ex1, IsNumeric([ex1]) AS ex2
FROM stat_converted;
The query returns two fields as it should but not evaluating correctly. Results with all numbers and others that are all letters are coming back as True(-1).
I also tried building a temp table and then applying IsNumeric to that with same results.
I also built a small test DB and the logic works so I am really confused.
IsNumeric will match things like "2E+1" (2 times ten to the power of 1, i.e. 20) as that is a number in scientific format. "0D88" is also a number according to IsNumeric because it is the double-precision (hence the "D") version of "0E88".
You could use LIKE '####' to match exactly four digits (0-9).
If you had more complex match requirements, say a variable quantity of digits, you would be interested in Expressing basic Access query criteria as regular expressions.

rle command counting changes in vector

n <- length(rle(sign(z)))
z contains 1 and -1. n should indicate the number of how many times the sign of z changes.
The code above does not lead to the desired outcome. If I expand the command to
length(rle(sign(z))[[1]])
it works. I don't understand the underlying mechanism of how [[1]] solves the problem?
rle returns a list consisting of two components: lengths, and values. As such, its own length is always 2. By contrast, you want to know the length of either of those components (they obviously have the same length). So either length(rle(…)[[1]]) or length(rle(…)[[2]]) would work. Better to use the names instead of an index though, e.g.
length(rle(z)$lengths)
However, this won’t be the number of times the sign changes; rather, it will be the number of times the changes plus 1.

Set value for multiple variables at once?

While programming a sample program in my TI-84 calculator, I was wondering if there were a way to initialize multiple variables to a single value in one line?
I tried doing
0 -> A,B,C,D
However this did not work for the calculator. I know you can do each one on an individual line, but my question is, is it possible to initialize multiple variables to a single value at once in TI-BASIC?
No. You cannot initialize multiple variables to a single value at once in TI-Basic.
However, you can:
Set all values in an array or matrix to the same value at once.
Initialize variables to zero in three bytes instead of four using DelVar (variable) and then leaving off the following colon. For example, instead of doing :0->A:0->B:Disp A,B you can do :DelVar ADelVar BDisp A,B.
Remember that uninitialized variables are treated as zero when first used.
No, you can't set multiple variables at once with one value. But if you just want them on the same line, you can use a colon like this 0→A:0→B. If you really feel like it, you could make another program that zeroes out every variable and just call it from within your program like this.
PROGRAM:ZERO
:0→A
:0→B
:0→C
...
PROGRAM:OTHER
:prgmZERO
...

Markov Algorithm for Random Writing

I got a litte problem understanding conceptually the structure of a random writing program (that takes input in form of a text file) and uses the Markov algorithm to create a somewhat sensible output.
So the data structure i am using is to use cases ranging from 0-10. Where at case 0: I count the number a letter/symbol or digit appears and base my new text on this to simulate the input. I have already implemented this by using an Map type that holds each unique letter in the input text and a array of how many there are in the text. So I can simply ask for the size of the array for the specific letter and create output text easy like this.
But now I Need to create case1/2/3 and so on... case 1 also holds what letter is most likely to appear after any letter aswell. Do i need to create 10 seperate arrays for these cases, or are there an easier way?
There are a lot of ways to model this. One approach is as you describe, with an multi-dimensional array where each index is the following character in the chain and the final result is the count.
# Two character sample:
int counts[][] = new int[26][26]
# ... initialize all entries to zero
# 'a' => 0, 'b' => 1, ... 'z' => 25
# For example for the string 'apple'
# Note: I'm only writing this like this to show what the result is, it should be in a
# loop or function ...
counts['a'-'a']['p'-'a']++
counts['p'-'a']['p'-'a']++
counts['p'-'a']['l'-'a']++
counts['l'-'a']['l'-'e']++
Then to randomly generate names you would count the number of total outcomes for a given character (ex: 2 outcomes for 'p' in the previous example) and pick a weighted random number for one of the possible outcomes.
For smaller sizes (say up to 4 characters) that should work fine. For anything larger you may start to run into memory issues since (assuming you're using A-Z) 26^N entries for an N-length chain.
I wrote something like a couple of years ago. I think I used random pages from Wikipedia to for seed data to generate the weights.

VB script math function

I have an ASP page where I have 2 variables, strActualRate and strProposed.
The values are:
strActualRate = 33.30
strProposed = 33.3
So when I write the following line to compare:
if strActualRate <> strProposed then
Response.Writr "Both are not equal!"
end if
I am getting the output "Both are not equal", even though both are the same.
I am sure that I need to use some mathematical conversion function to compare.
Can anyone tell me how to solve this ?
Thanks in advance!
If I understand correctly, you think the two values are equal but because VBScript is comparing strings rather than numbers the two are coming back as not equal.
You're correct in the conversion idea, and here's the code:
if CDbl(strActualRate) <> CDbl(strProposed) then
Response.Write "Both are not equal!"
end if
That will convert your string values to numbers to do the comparison.
Your question doesn't really add up, so I'm not really sure what the problem is. I will try to clear up some things about data types and comparison.
You are using the prefix "str" for your variables which suggests that you intend to store string values in them, however you are instead storing numeric values in them. Either you are confused about how hungarian notation is used to keep track of the data type, or the code that you posted does not look like the code that you are actually using.
The numeric value 33.30 is exactly the same as the value 33.3. If you instead would have used the string values "33.30" and "33.3", they would be two strings that are not equal.
If your code is corrected (Response.Write instead of Response.Writr) so that it runs, it will not produce any output at all. As the values are equal, the condifion in the if statement evaluates to false.
If you do in fact assign string values to the variables, the code would output "Both are not equal!". This is just as expected as the strings are not equal. If you have strings and want to compare them as numerical values, you have to comvert them:
If CDbl(strActualRate) <> CDbl(strProposed) Then
Response.Write "Both are not equal!"
End If
Try casting the values to a double in the comparison statement with CDbl()
Are you intending to perform the comparison as strings, floating point numbers or some other method? If you are comparing them as strings, then clearly they are not equal, as one of them has an extra zero on the end. If you are comparing them as floating point numbers, then you generally want to use a comparison that involves taking the difference and checking that it is smaller than some small value. This is because floating point number calculations involve some degree of inaccuracy and comparisons between them can fail because of the underlying representation.

Resources