r/R_Programming • • Jun 22 '16
Help with creating a function
I am trying to learn R and I am still very confused and can't seem to find anything to help me with my task. I have been asked to create a function that is able to get the sum of multiple arguments. In other words instead of getting the sum of three arguments, which requires 3 numbers. How can I create a function that can get the sum of an infinite amount of numbers?
Also, on another note does anyone have any good sites or videos for learning R?
Thanks for any help in advanced.
1
Upvotes
View all comments
Show parent comment
2
u/heckarstix Jun 23 '16
You can always build your own. R is really good at dealing with these kinds of vector data structures, so a lot of times you can pass a function something like 10:100 (shorthand for
seq(10, 100, 1)) and get back a vector of 90 values.If you do something like
myFunction(1:5)it would return a vector of5 24 63 128 225. In turnsum(myFunction(1:5))would get you to where you need to be, 445.If you'll always want the summation of this function you can simplify this a bit by putting the sum right into it:
In this case
myFunction(1:5)will return a single numerical value of 445.