Hello
Have a problem with a function I'm trying to implement in order to calculate
weighted means on semi-aggregated data since tapply doesn't work.
I've found a good solution to this using the following syntax:
splitted.value<-with(dataframe,split(value_var,group_var))
splitted.freq<-with(dataframe,split(frequency_var,group_var))
mapply(weighted.mean,splitted.value,w=splitted.freq)
Using these commands interactively produce the correct results
When implementint these commands as a function I run into problems however.
I've tried two versions:
1)
group.weightmean<-function(dataframe,value_var,group_var,frequency_var){
splitted.value<-with(dataframe,split(value_var,group_var))
splitted.freq<-with(dataframe,split(frequency_var,group_var))
mapply(weighted.mean,splitted.value,w=splitted.freq)
}
This version works but only if I first do an "attach(dataframe)" which
I want to avoid. The attach command is not necessary when running the commands
interactively
2)
group.weightmean<-function(dataframe,value_var,group_var,frequency_var){
splitted.value<-with(dataframe,split(dataframe$value_var,dataframe$group_var))
splitted.freq<-with(dataframe,split(dataframe$frequency_var,dataframe$group_var))
mapply(weighted.mean,splitted.value,w=splitted.freq)
}
This version does not run and creates an error message involving "that
input data is not a vector"
Can anyone help out?
[[alternative HTML version deleted]]