Is your data in data.table format? It is not, but just so you know how to test if it is in the future, run the following line of code:
is.data.table(df)
It should output FALSE
In order to counteract this, add the following before your code:
library(data.table) #import data.table package
df = as.data.table(df) #convert df to data.table
df[, t_Product := .N , by = .(Product)] #your code
OR
library(data.table)
setDT(df)
df[, t_Product := .N , by = .(Product)]
Example with Palmer Penguins
library(data.table)
library(palmerpenguins)
data(package = 'palmerpenguins') #importing the palmer penguins data
df = as.data.table(penguins) #you can also use setDT(df)
df[, t_Product := .N , by = .(species)] #using your code here
df[, .(species, island, t_Product)] #selecting columns using column name
#> species island t_Product
#> 1: Adelie Torgersen 152
#> 2: Adelie Torgersen 152
#> 3: Adelie Torgersen 152
#> 4: Adelie Torgersen 152
#> 5: Adelie Torgersen 152
#> ---
#> 340: Chinstrap Dream 68
#> 341: Chinstrap Dream 68
#> 342: Chinstrap Dream 68
#> 343: Chinstrap Dream 68
#> 344: Chinstrap Dream 68
Created on 2020-08-10 by the reprex package (v0.3.0)