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') Weight<-c('5','20','3','2','15','21','13','8') Data <- data.frame(Country, Landvalue, Temperature_spring,Temperature_summer, 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 Belgium. 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! ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.