Skip to content

weighing proportion of rowSums in dataframe

2 messages · D. Alain, Eik Vettorazzi

#
Hi Alain,
here is a one-liner for a df without the rowSum column

df<-data.frame(id=c("x01","x02","x03","x04","x05","x06"),a=c(1,2,NA,4,5,6),b=c(2,4,6,8,10,NA),c=c(NA,3,9,12,NA,NA))

(df$wSum<-apply(sweep(df[,-1],1,rowSums(df[,-1],na.rm=T),"/"),1,function(x)sum(x*w,na.rm=T)))

hth.

Am 06.02.2013 14:17, schrieb D. Alain: