I am new in R application. I need a function to create a new variable, say Y_(i,t) by demeaning X_(i,t) variable in a panel data having time, T =18 and countries, n = 48. That is Y_(i,t) = X_(i,t) - x_bar(i) Where x_bar(i) is country specific mean. Each country mean is subtracted from the corresponding country values, X from 2001 to 2018.
The data frame below is an abridged version that mimic the actual data description above.
pdat <- data.frame( year = rep(c(2001,2002,2003,2004,2005),5),
code = rep(c('GHY', 'DRF', 'JYU','HYU','POI'),each=5),
X = c(60,70,400,300,15,20,200,150,61,71,401,301,400,300,61,71,401,67,145,678,123,456,456,875,246))
pdat <- plm::pdata.frame(pdat, index=c("code", "year"))
pdat
The package you are already using, plm
, provides a function to perform what you describe, a within transformation (using in, e.g., fixed effect panel model estimation).
Continuing your example, you can simply do:
Within(pdat$X)