Let's say I have the following vector:

```
x <- c(5, 6, 3, 7, 5, 2, 6, 7, 5, 3, 1, 5, 6)
```

I would like to create a function with the parameter n that produces the sum of the first n elements.

Skip to content
# R – Sum of first n elements of a vector

rsubsetsumvector

Let's say I have the following vector:

```
x <- c(5, 6, 3, 7, 5, 2, 6, 7, 5, 3, 1, 5, 6)
```

I would like to create a function with the parameter n that produces the sum of the first n elements.

## Best Solution

How about this?