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.
Related
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.
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 6 years ago.
Improve this question
I'm programming some financial software needing to program not very complicated mathematical formulas.
After writing the code, it is not readable anymore - i.e - you can't easily discern what was the original formula.
What would be a good way to program mathematical formulas so they could be easily read later on?
For example, programming a calculation for a loan with a fixed interest rate:
(TotalValue*monthlyInterest*Math.pow((1+monthlyInterest),totalPayments))/(Math.pow((1+monthlyInterest),totalPayments) - 1)
Though using meaningful variables, the formula is not readable. But if you will look at this formula written in a classical mathematical notation on a page - you will easily know what's going on (really basic math).
How would you even take this formula and write it in a readable way.
Clarification
I'm not talking about any specific language. This should be the same for any high-level language.
The example uses Javascript.
You could also add local variables that are abbreviations of the long name. As they are local, their definition will be right above the formula. This could look like
TV = TotalValue;
mi = monthlyInterest;
N = totalPayments;
MR = ( TV*mi*Math.pow( 1+mi, N ) ) / ( Math.pow( 1+mi, N ) - 1)
MonthlyRate = MR;
where you then see that you can simplify the formula to
MR = (TV*mi) / ( 1 - Math.pow( 1+mi , -N) - 1)
As far as i know, it is not possible to convert Excel formulas to equations
But for clarity, you could recreate the Equation in the Equation Editor and put it next to the cell. This way it is immediately obvious what you are doing.
Another way would be to create the Mathematical Equation and add a macro. This way you could calculate your values in VBA.
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 6 years ago.
Improve this question
In languages such as Java and Eiffel, there is a divide between functions (returning a value) and methods, groups of transformative statements. So a method would be something like:
public main blah NAMEOFMETHOD(x) {
STATEMENT/COMMAND
STATEMENT/COMMAND
STATEMENT/COMMAND
}
R has functions as basic to the language, but there is no obvious way of making a method (I think). Do I just put groups of commands in a class? (I programmed years ago with Eiffel and Java, but am now using R for statistics and am extending my work with some programming - or is it coding you call it now?!)
Old guy relearning. Please be gentle.
R is a functional programming language. It's important to understand that there is technically no real difference between a function (with possible side-effects such as generating a plot) and a method.
The only difference is the absence of a "return value".
A Java method with signature
void doSomething()
(wich misses a return) can be implemented in R as
doSomething <- function(){
# Do something here
return(invisible(NULL))
}
where NULL is the return value ("nothing") and invisible instructs R not to automatically print this result to the console.
return(invisible(NULL)) is equivalent to the shorter statement return(invisible()) and, if it's the last line of code in your function, as it should be, also equivalent to invisible() because R auto-returns the result of the last computation in the function body.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to understand how really works this script :
y <- y[keep, , keep.lib.sizes=FALSE]
in :
keep <- rowSums(cpm(y)>1) >= 3
y <- y[keep, , keep.lib.sizes=FALSE]
I do know d.f[a,b] but I can not find R-doc for d.f[a, ,b].
I tried "brackets", "hooks", "commas"... :-(
(Sometimes I would prefer that one does not simplifie his R script !)
Thanks in advance.
Subscripting data.Frames takes two values: df[rows, columns]. Any third value are optional arguments that you can use to subscript.
The most common of those is drop=FALSE as in df[1:18, 3, drop = FALSE]. This is done because when you subset just one column of a data.frame, it will lose the data.frame class. In your specific case, it seems like you are using another object that looks like a data.frame but with added functionalities from the bioconductor package. A look at the methods for those will tell you how these work.
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?