[R] Weighted demean by group on only a selection of the dataset

Janka VANSCHOENWINKEL janka.vanschoenwinkel at uhasselt.be
Thu Dec 24 14:03:12 CET 2015

Dear colleagues,

I am trying to find a simple code to demean
1) only certain values of a dataset,
2) by group
3) and in a weighted fasion.

Currently, I can only demean all the numeric variables in the dataset:

Data[,sapply(Data,  is.numeric)] <- apply(Data[sapply(Data,
is.numeric)], 2, function(x) scale(x, scale = FALSE))

Assume that my dataset looks like this:
Country<- c('BE','BE','DE','GR','IT','ES','DE','NL')
Landvalue<- c(21000, 23400, 26800, 15000,18000,23000,19000,23000)
Temperature_spring <- c('15','16','14','18','23','21','12','15')
Temperature_summer <- c('25','18','19','23','24','22','15','19')
Temperature_autumn <- c('14','12','12','10','20','20','11','13')
Temperature_winter <- c('9','4','12','14','15','13','17','12')
Data <- data.frame(Country, Landvalue,
Temperature_autumn,Temperature_winter, Weight)

Now imagine I only want to demean the temperature-variables, grouped
by country and weighted by weight. With grouped by country I mean that
I want to subtract only the mean of Belgium from an observation in

Does somebody know how to add the three functions to the code line I
already have? Or if this does not work, what code should I use?

Thank you very much and have a nice Christmas!

More information about the R-help mailing list