Hi, I am currently experiencing an exponential increase in time it takes to perform a command using tidyverse package.

Consider the following structure (simplified):

```
data <- data.frame(name = c("a","b","c","d","e","f"),
ID =c(1,1,1,2,2,2),
sales = c(100, 250, 300, 50, 600, 390),
t = c(0.1,0.3,0.4,0.05,0.15,0.2),
n=c(1,2,3,1,2,3),
correct_result = c(-221.4,-27.8,69.1,-143.71,-19.11,43.19))
data$ID <- as.integer(data$ID)
```

I found that it is more efficient to group by ID as integer, rather than factor.

The formula I am trying to calculate implies that for a given name, say, "a", I want to take the sum of sales of all other related names (by their ID) and divide by 1-t for the respective names.

To get a sense of what I am trying to compute for each ID & and name:

```
(data$sales[2]/(1-data$t[2]))*(data$t[1]-data$t[2]) + (data$sales[3]/(1-data$t[3]))*(data$t[1]-data$t[3])
(data$sales[1]/(1-data$t[1]))*(data$t[2]-data$t[1]) + (data$sales[3]/(1-data$t[3]))*(data$t[2]-data$t[3])
(data$sales[1]/(1-data$t[1]))*(data$t[3]-data$t[1]) + (data$sales[1]/(1-data$t[1]))*(data$t[3]-data$t[1])
```

```
library(tidyverse)
# The Model:
data <- data %>%
mutate(ovt=sales/(1-t))
sumforgoup1 <-function(forname , groupid){ # Create the function:
key_t <- dplyr::filter(data,
ID == groupid,
name==forname) %>% pull(t)
temp <- dplyr::filter(data,
ID == groupid,
name!=forname) %>% mutate(diff_key_t=
key_t - t)
sum(temp$ovt*temp$diff_key_t)
}
mutate(rowwise(data),
result = sumforgoup1(name,ID)) # Store result in a new column.
```

So, the function works fine in this dataset. However, when i apply this function over a larger dataset with, say, 300 rows, the formula takes approximately 6 seconds. Increasing the number of rows with 300 more (i.e., 600 rows) it takes around 35 seconds..

I have around 30.000 rows, so this would take hours..

In the full dataset i converted ID to factor so you can get a sense of the levels (sub here = name):

```
$ ID : Factor w/ 9097 levels "1","2","3","4",..: 1 2 2 3 4 5 5 5 5 5 ...
$ sub : Factor w/ 40 levels "1","2","3","4",..: 1 1 2 1 1 1 2 3 4 5 ...
```

Any recommendations/tips is appreciated,

Thanks!