Is using for-statements in R considered bad style? [closed] - r

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
R is a functional programming language. Many for-statements can be replaced by one of the apply-functions. Thus, isn't the for-statement against the functional programming paradigm? Is using for-statements considered bad style, in the sense of functional programming?

Yes, a for loop is against the functional programming paradigm. However, R is not a pure functional programming language. It allows side effects.
There are scenarios where a for loop is appropriate. In particular, if you don't need a return value, but only a side effect such as plotting or exporting files, for loops are more appropriate than *apply functions.
Then there are some tasks that a just easier to solve with a for loop. E.g., if you look at the source of the Reduce function you'll find a for loop.

Related

Recursion When to use it? [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 6 years ago.
Improve this question
So we just finished the subject recursion in school and I am still wondering "why?".
I feel like I have just learned a hell of a lot about math in a programming way with the sole purpose of passing an exam later on and then never again.
So what I want to know is when to use it? I can only find people saying "when you want to call a function within itself" but why would you do that?
Recursion is the foundation of computation, every possible program can be expressed as a recursive function (in the lambda calculus). Hence, understanding recursion gives you a deeper understanding of the principles of computation.
Second, recursion is also a tool for understanding on the meta level: Lots of proofs over the natural numbers follow a pattern called "natural induction", which is a special case of structural induction which in turn allows you to understand properties of very complex systems in a relatively simple way.
Finally, it also helps to write good (i.e. readable) algorithms: Whenever there is data to store/handle in a repetitive calculation (i.e. more than incrementing a counter), you can use a recursive function to implicitly manage a stack for you. This is also often very efficient since most systems come with a machine stack at hand.

Proper Coding Techniques in R [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 7 years ago.
Improve this question
I am writing an R code that has many different functions that eventually I will want to use all together on different data sets.
As I keep building functions it seems to be getting harder to keep track of everything in my script.
My question is, is it proper R coding to break functions into separate R Scripts or should it all be in one massive script?
Thank you for your help. This is my first time trying to code something this large!
-B
Yes, you can store your functions in multiple R scripts.
If you need to call them, you can use source().
For eg:
Say you have func1 , func2 saved in myfunc.R.
To call them,
source('myfunc.R')
#other codes
func1()
func2()
As to whether this approach is recommended, depends on your project requirements.
Alternatively, you can consider packaging them as recommended by Richard.

Best Method of Working With Number(s) Over 1*10^100000000 [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 8 years ago.
Improve this question
I am well aware this is a crazy thing to ask, but what method and or language should I look towards to work with numbers of over 1*10^100000000? I know doubles and such have much smaller limits in accuracy, but my purpose is to apply math such as division, addition, etc to these numbers.
Different languages use different library to support arbritrary-precision arithmetics. Most modern programming language offer support for it in one way or another.
Take a look at: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Languages
What you are looking for is called arbitrary-precision arithmetic. Typically there are separate packages for integers and for floats. Some languages have these built-in; for example Maxima [1] has both integers and floats. Some other languages (e.g. Common Lisp) have only integers built-in. For other languages such as C and C++, you can get libraries.
[1] http://maxima.sourceforge.net, http://sf.net/p/maxima

Can you provide a specific situation illustrating when a loop in R could be preferred to an apply function? [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
Can you please provide a specific situation illustrating when a for loop might work more effectively than the more commonly cited apply suite of solutions?
If the results of the previous computation are used in the next computation, it is appropriate to use a for loop, since this behavior is difficult to replicate with lapply (you would have to use something like Reduce). R is not necessarily slow with for loops, merely with memory allocation (which is easy to get wrong with for loops). See Chapter 2 of the R Inferno.

is Haskell suitable for statistic analysis [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 9 years ago.
Improve this question
The question is in the Title. Basically I'm looking for an alternative to R.
I've been using R a bit, there are some really good stuff about it (especially data.frame plyr and ggplot), however I really love Haskell and type inference, so I was wondering if using Haskell to do "simple" statistic analysis would be a good choice.
My basic needs are :
read/write CSV
import SQL table
do some basic 'mapReduce' on the data. Which where R is great but I assume Haskell should be equally good.
However my experience with Haskell is everything is fine until you process realworld data. You always encounter performance issue (and soonish) because even though in theory you should write functional code and don't worry about what's the computer is doing, if you don't and don't use the appropriate library and are not an Haskell expert, stuff are damned slow.

Resources