Question: I have a dataframe like this:

```
df = data.frame(
  x = 1:100,
  y = rep(1:10, times = 10, each = 10)
) %>% 
  group_by(y)

```

And I would like to compute the sum of `x` from the 3rd to the 6th row of each group of `y`.
I think this should be easy, but I just can not figure it out at the moment.

In pseudocode I imagine something like this:

```
df %>% 
  mutate(
    sum(x, ifelse(between(row_number(), 3,6)))
  )

```

But this of course does not work. I would like to solve it with some `dplyr`-function, but also in base R I cannot think of a fast solution.
For the first group the sum would be `3+4+5+6`....

Answer: 
Explanation
-----------

[`Random#ints`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Random.html#ints(long,int,int)) returns `IntStream`, not a `Stream<Integer>`, that is the problem.

`IntStream` is a special class to represent primitive `int`s. But generics in Java do not support primitives, i.e. there is no `List<int>`. So you first have to make your `int`s `Integer`s, the wrapper class. Then you can collect to `List<Integer>`.

---

Solution
--------

Just **box** it and it works.

```
stream.boxed().toList() // since Java 16
// or
stream.boxed().collect(Collectors.toList())

```

Also see the documentation of [`IntStream#boxed`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/stream/IntStream.html#boxed()):

> 
> Returns a Stream consisting of the elements of this stream, each boxed to an Integer.
> 
> 
> 

For understanding, `boxed()` is roughly equivalent to

```
stream.mapToObj(x -> Integer.valueOf(x))

```

---

So in your case:

```
public static void main(String[] args) {
    System.out.println(
        new Random()
            .ints(10, 0, 20)
            .boxed()
            .toList());
}

```
**Just use .boxed() in your IntStream reference.**

for eg:

```
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Java8 {
    public static void main(String[] args) {
        IntStream ints = new Random().ints(10, 0, 20); //here ints is your intstream
        List<Integer> collect = ints.boxed().collect(Collectors.toList()); 
/*
  Here Just use ints.boxed() and then collect to list 
*/
        collect.forEach(System.out::println);
    }
}

```