i trying aggregate columns of data frame. in data frame each column correponds industry , each row particular country. idealy aggregate columns weighted average. small fraction weights missing. in case r aggregate country industries simple average. snipet data frame (these exemplary weights other columns)
| mining | food | weight85| weight90.93| |:----------:|-- -----:|---------:|------------| | 0.9608709| 0.8839236| 0.2738525| 0.1943577| | 0.6445055| 0.8483874| 0.2958678| 0.1043844| | 0.6977353| 0.9449249| na| na| | 0.7970192| 0.5941056| 0.2324452| 0.1904089| | 0.7261323| 0.6333187| na| na| | 0.9959837| 1.0101725| 0.3872314| 0.1628354| i compute weighted average when ingoring missing values problem follows:
ggpc$mining.weighted <- ggpc$weight85*ggpc$mining ggpc$food.weighted <- (1-ggpc$weight85)*ggpc$food ggpc$food.mining<- rowsums(ggpc[,54:55], na.rm=t)
building on answer mts provided. came following solution solution, computes 1 row either simple average or weighted average.
if(sum(is.na(df[1,37])>0)) {1/2*df[1,5]+1/2*df[1,6]} else { df[1,37]*ggpc[1,5]+(1-df[1,37])*df[1,6]} and further looping through rows of data frame
df$data.column.agg <- 0 (i in 1:length(df)) { df[i,*data.column.agg*] <- if(sum(is.na(df[i,*weight column*])>0)) {*simple average* } else {df[i,*weight column*]*ggpc[i,*data column1*]+(1-df[i,*weight column*])*ggpc[i,*data column2*]} }
Comments
Post a Comment