Use 'not' instead of '!' in R [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I would like R to be more pythonic. One of the steps for transforming it would be the use of 'not' instead of '!'. I hope this does not need writing of C++ and compiling etc, or does it?
Functionality: "!TRUE" should equal "not TRUE".
If you know where to start, please point me there.

You better embrace the R way. Why all the effort for such a small thing? If you want Python, stick with Python. Besides, it is REALLY common to have ! act as "not" in other programming languages.
Anyway, this is something that allows the use of "not", but it will involve parentheses (the Arrr! way)
not <- function(x) { !x }
if (not(FALSE)) { print(1) }
[1] 1
In my opinion this is far worse than !.
And I can hear you thinking "but I would like to have if x is not in y:", but please, that would be the same as suggesting I'd want a way for Python not to need indenting.
The idea is that you learn a language, you do not change a language to suit an individual's needs. What will happen with others using your code?

Related

Coding style: Is this a good way to write a function [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Is the following code example, calling a function in the parameters line of another function, acceptable? Or will I run in problems somehow?
newfunction <- function(df = merge_two_df(), size = define_size()){
... do something
}
This is basically a question of what you gain and lose compared to the alternatives.
So first let's look at the alternatives:
Option1
newfunction <- function(){
df = merge_two_df(), size = define_size()
... do something
}
Option2
newfunction <- function(df,size){
... do something
}
newfunction(merge_two_df(),define_size())
Comparing your status-quo to option 1 shows that your current way is more flexible. You have defined default values for each parameter but retain the option to use other parameters. So there is nothing to gain hard-coding the parameter definition into the body of the function.
Now comparing to option 2, your current way is actually more standardized because you have defined defaults. You can simply and cleanly call your function without any parameters while having the same flexibility of switching up parameters if you want. However on the flip side you are a bit less explicit in calling these parameters which might be bad in terms of documentation and clarity when rereading the code.
If this function is part of a module that might be used/exported across several projects some even done by someone other then yourself, then I'd argue for option 2 to be simpler and clearer.
Otherwise what you do right now works best.

Why is 'DO LOOP' missing in 328eForth? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I’m trying to learning Forth directly in an embedded system and using Starting Forth by Leo Brodie as a text. The Forth version I’m using is 328eForth (a port of eforth to the ATmega328) which I’ve flashed into an Arduino Uno.
It appears that the DO LOOP words are not implemented in 328eForth - which puts a kink in my learning with Brodie. But looking at the dictionary using “WORDS” shows that a series of looping words exist e.g. BEGIN UNTIL WHILE FOR NEXT AFT EXIT AGAIN REPEAT amongst others.
My questions are as follows:
Q1 Why was DO LOOP omitted from 328eForth?
Q2 Can DO LOOP be implemented in other existing words? If so, how please and if not why? (I guess there must be a very good reason for the omission of DO LOOP...)
Q3 Can you give some commented examples of the 328eForth looping words?
Q1: A choice was made for a different loop construct.
Q2: The words FOR and NEXT perform a similar function that just counts down to 0 and runs exactly the specified number of times, including zero.
The ( n2 n1 -- ) DO ... LOOP always runs at least once, which requires additional (mental) bookkeeping. People have been complaining
about that as long back as I can remember.
Q3: The 382eforth documentation ForthArduino_1.pdf contains some examples.
Edit: Added some exposé to Q2

Properly reading recursive functions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I currently teach myself programming and I came to a point where I was introduced to the recursive functions. I understand the basic principle behind them but whenever I try to read a code which contains recursive functions I have tough times tracing them. Honestly, If I don't write down the whole function on a paper and manually follow it I can't really understand what it does, I can't follow it mentally is what I am saying. Can you give some tips on how to do it mentally? I am pretty much average intelligent ( IQ 117 ), maybe that's the problem? Thanks in advance.
I think this is an excellent question. Many people make the mistake of trying to follow the call-stack of a recursive function, which is the wrong approach.
I find the only way is to look at what is written and ask yourself whether it is logically correct and whether it will end. If those things are true, it is going to work and you don't really need to understand how.
In more detail:
Are all possible input values considered?
Are the statements true in each case?
Is there a (are there) value(s) at which point the function will not recurse?
Do all remaining input values ultimately lead to that value?
For example (pseudo code):
factorial(n):
if (n < 0) throw InvalidArgument
if (n == 0) return 1
else return factorial(n - 1) * n
All input values are covered. The statements are true. There is a value at which it does not recurse (<=0) and the all remaining values reduce by 1 until the exit value is reached. Therefore it works.
You may of course make mistakes in your thinking. A good IQ does help here, and you may need at some point to resort to tracing or debugging to check your assumptions and find the error in your logic, but this should not be the first approach.
That is the beauty of recursive solutions: they tend to be statements of fact rather than procedural instructions!
I think IQ does not matter. Anyway, i think it's all about practice. Try to write some recursive functions. And after alot of practice and experience you will find it more easy to understand.
There is some good ideas.
Good luck.

How to graph an equation in the form z=x*y (programme needed) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i would like to graph this kind of equation, z=x*y. I would like to set intervals like -n< x <-n and so on, to see how z varies in function of x and y at the same time. I also need to check the value of z at given x and y and see where z is at its maximum, for example. Thanks and have all a nice day!
Edit: the equation i am trying to study is z=x * (164+0.25y)
where 0 < x < 2.5 and 0 < x < 700.
P.S. I don't understand the downvotes, I kindly asked for a question in a polite way, providing as many details as I could.
This question may be a little outside of the scope of this site, but in regards to a graphing program:
One of the easiest ways to determine things about graphs is to use Wolfram Alpha a computational knowledge engine. A quick tour on the site and just trying a few things out should get you to where you want to go pretty quickly.
Since you appear new to this site, check out some of the Stack Overflow Basics and be sure to up-vote any help you get or questions you may find helpful.

Which program to solve integration = 0 for a variable? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
i'd like to know how to solve a definite integral in Mathematica.
I do know all variables except b, and need to solve for F(b)=0.
How can i solve it in Mathematica?
Here is my try:
NSolve[Integrate[1/(8*(1 - ff) (2 Pi)^0.5) E^(-0.5*((x - 1.1)/(1 - ff)/8)^2), {x, 0, 9999}] == -0.44531779637243296, ff]
These integrals can be trivially expressed in terms of an error function: Wiki, Mathworld. Hence what you need here is a library to (i) calculate error functions, (ii) numerically solve non-linear equations. Virtually any language has this, so pick anything you're familiar with. In Mathematica, look up Erf and NSolve.
I'd start by plugging it into Wolfram Alpha and see what it gives you.
Mathematica should be able to do it. I think of statistics first when R comes up; I don't know about its calculus capabilities. Excel is not the first choice.
If I were you, I'd be less worried about the software and more worried about the solution itself. A function of this form might be well known. Plot each one and visually check to see what the functions look like and how easy they might be to integrate.
Like this:
http://www.wolframalpha.com/input/?i=graph+exp%28-%28%28x%2B5%29%2F1.5%29%5E2%29
You should be wondering why it's three similar looking integrals. Those singularities in the plot tell you why.
If there's no closed form solutions, you'll have to go with a numerical one. You'll have to choose an algorithm (simple Euler or Runga Kutta or something else), interval sizes, etc. You'll want to know about singular points and how best to tackle them.
Choosing a package is just the start.
You might find http://r.789695.n4.nabble.com/calculus-using-R-td1676727.html helpful.

Resources