Jay khan Jay khan - 25 days ago 8
R Question

Perform operation in Data table based on more than one constraint in R

Normally when I perform operations on data table its on one constraint e.g

setDT(df)[, mean.price := mean(Price), by = Id]


this command get the mean of price based on unique
Id
. My question is that if there is a way to mention two constraints in the command e.g

setDT(df)[, mean.price := mean(Price), by = Id & Year]


So I can get the mean of rows with same
Id
belonging to same
Year
.

Suppose I have following data frame

Id Price Year
1 5 2003
1 10 2003
1 4 2003
1 6 2004
1 6 2004
1 10 2004
2 7 2003
2 10 2003
2 3 2003
2 2 2004
2 3 2004
2 10 2004


so the resultant data frame will look like

Id Price Year Mean.Price
1 5 2003 6.33
1 10 2003 6.33
1 4 2003 6.33
1 6 2004 7.33
1 6 2004 7.33
1 10 2004 7.33
2 7 2003 7
2 10 2003 7
2 4 2003 7
2 2 2004 5
2 3 2004 5
2 10 2004 5

Answer

You can either write it like akrun wrote it:

setDT(df)[, mean.price := mean(Price), by = .(Id,Year)]

or with the names as vector:

setDT(df)[, mean.price := mean(Price), by = c("Id","Year")]
Comments