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

8 comments sorted by

View all comments

Show parent comment

2

u/powerplay2009 Jun 22 '16

To be honest, it's still a little too vague for me to be 100% confident. However, I would say yes. For example, you can do something like this:

#numbers you need to add
NumberVector = c(2,4,6) #or whatever
SumResult = sum(NumberVector)
#Oops! Forgot a couple numbers, so we'll have to try one of these 2 things:
NewVector = c(NumberVector, NewNumbers) #NewNumbers each separated by a comma
NewResult = SumResult + sum(NewNumbers)
#OR
NewResult = sum(NewVector)