グループごとに一連の変数の加重平均を計算したいので、colwise を使用した ddply は論理的な選択のようです。私のコードは次のとおりです。
library(plyr)
j.parms.by.meas <- ddply(j.subgroups,
.(measure),
colwise(weighted.mean,
.(ee.eer, ee.iplv, sf.per.tstat, kwh.per.sf,
ee.kw.per.sf, ee.kwh.per.sf, efficiency,
motor.eff, ee.lpd, inc.cost),
j.subgroups$unit.qty))
ただし、次のエラーが発生します。
Error in weighted.mean.default(X[[1L]], ...) :
'x' and 'w' must have the same length
意図したように、グループ化変数のサブセットの unit.qty のベクトルではなく、unit.qty のベクトル全体を加重平均関数に渡しているようです。ddply で生成されたサブセット ベクトルをパラメーターとして ddply の指定された関数に渡す簡単な方法はありますか?
以下にいくつかのサンプル データを示します。すべての変数がすべてのメジャーに入力されるとは限らないことに注意してください。
measure ee.eer ee.iplv sf.per.tstat kwh.per.sf ee.kw.per.sf ee.kwh.per.sf efficiency motor.eff ee.lpd inc.cost unit.qty
9813 High Performance Glazing NA NA NA NA 0.0004788 1.634573 NA NA NA 3.177014 817.00
9814 High Performance Glazing NA NA NA NA 0.0016200 6.031441 NA NA NA 0.889816 5051.00
9815 High Performance Glazing NA NA NA NA 0.0011559 4.607375 NA NA NA 1.502042 836.00
9816 High Performance Glazing NA NA NA NA 0.0006050 2.286861 NA NA NA 1.041064 4027.00
9817 High Performance Glazing NA NA NA NA 0.0008772 3.574747 NA NA NA 2.436068 173610.00
9818 High Performance Glazing NA NA NA NA 0.0010797 4.288593 NA NA NA 1.373322 5328.00
9819 High Performance Glazing NA NA NA NA 0.0010797 4.288593 NA NA NA 1.373322 5668.00
9820 High Performance Glazing NA NA NA NA 0.0010797 4.288593 NA NA NA 1.373322 150.00
9821 High Performance Glazing NA NA NA NA 0.0012480 4.887974 NA NA NA 1.190700 33735.40
9822 High Performance Glazing NA NA NA NA 0.0014988 6.041895 NA NA NA 2.081282 3879.50
9823 High Performance Glazing NA NA NA NA 0.0012100 4.736534 NA NA NA 1.126340 69.70
9824 High Performance Glazing NA NA NA NA 0.0010308 4.307727 NA NA NA 2.693508 1356.03
9825 High Performance Glazing NA NA NA NA 0.0009654 3.810419 NA NA NA 1.180242 1832.00
9826 High Performance Glazing NA NA NA NA 0.0009654 3.810419 NA NA NA 1.180242 2748.00
9827 High Performance Glazing NA NA NA NA 0.0011123 4.275836 NA NA NA 0.493198 1722.90
9828 High Performance Glazing NA NA NA NA 0.0008511 3.332246 NA NA NA 0.987162 1850.00
9829 High Performance Glazing NA NA NA NA 0.0011237 4.651937 NA NA NA 2.382166 224.00
9830 High Performance Glazing NA NA NA NA 0.0010797 4.288593 NA NA NA 1.373322 819.00
9831 High Performance Glazing NA NA NA NA 0.0014532 6.323424 NA NA NA 3.401468 3934.00
9832 High Performance Glazing NA NA NA NA 0.0011559 4.607375 NA NA NA 1.502042 9351.00
10329 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 900.00
10330 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 1246.00
10331 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 265.00
10332 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 308.00
10333 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 500.00
10334 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 335.00
10335 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 550.00
10336 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 433.00
10337 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 676.00
10338 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 800.00
10339 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 350.00
10340 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 750.00
10341 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 200.00
10342 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 1200.00
10343 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 600.00
10344 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 433.00
10345 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 500.00
10346 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 600.00
10347 >150 HP NA NA NA NA NA NA NaN 0.9496114 NA 117.631768 4988.00
10348 >150 HP NA NA NA NA NA NA 0.95 0.9500000 NA 117.631768 250.00
そして、これは同じデータの dput からのダンプです:
structure(list(measure = structure(c(122L, 122L, 122L, 122L,
122L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 122L,
122L, 122L, 122L, 122L, 122L, 124L, 124L, 124L, 124L, 124L, 124L,
124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L,
124L, 124L, 124L), .Label = c("(2) 4-lamp 4ft T5HO replacing 1000W MH",
"(2) 6-lamp 4ft T8 replacing 1000W MH", "2-foot lamp", "2-lamp 4ft T5HO replacing 150W HPS",
"2-lamp 4ft T5HO replacing 150W MH", "2-lamp 4ft T5HO replacing 175W HPS",
"2-lamp 4ft T5HO replacing 175W MH", "2-lamp 4ft T8 replacing 150W HPS",
"2-lamp 4ft T8 replacing 175W HPS", "2-lamp 4ft T8 replacing 175W MH",
"3-foot lamp", "3-lamp 4ft T5HO replacing 250W HPS", "3-lamp 4ft T5HO replacing 250W MH",
"4-foot lamp", "4-lamp 4ft T5HO replacing 400W HPS", "4-lamp 4ft T5HO replacing 400W MH",
"4-lamp 4ft T8 replacing 250W HPS", "4-lamp 4ft T8 replacing 250W MH",
"6-lamp 4ft T5HO replacing 400W HPS", "6-lamp 4ft T5HO replacing 400W MH",
"6-lamp 4ft T5HO replacing 750W MH", "6-lamp 4ft T8 replacing 400W HPS",
"6-lamp 4ft T8 replacing 400W MH", "8-foot lamp", "Automotive Facility",
"Cold Cathode Fluorecent Lamps 3 w", "Cold Cathode Fluorecent Lamps 5 w",
"Cold Cathode Fluorecent Lamps 8 w", "Compact flourescent Lamps 13 w Hardwired",
"Compact flourescent Lamps 14 w Hardwired", "Compact flourescent Lamps 14 w Screw-In",
"Compact flourescent Lamps 15 w Screw-In", "Compact flourescent Lamps 18 w Hardwired",
"Compact flourescent Lamps 18 w Screw-In", "Compact flourescent Lamps 20 w Screw-In",
"Compact flourescent Lamps 23 w Hardwired", "Compact flourescent Lamps 23 w Screw-In",
"Compact flourescent Lamps 26 w Hardwired", "Compact flourescent Lamps 26 w Screw-In",
"Compact flourescent Lamps 27 w Hardwired", "Compact flourescent Lamps 27 w Screw-In",
"Compact flourescent Lamps 32 w Hardwired", "Compact flourescent Lamps 7 w Hardwired",
"Compact flourescent Lamps 7 w Screw-In", "Compact flourescent Lamps 84 w Hardwired",
"Court House", "Daylighting Controls", "Dining: Bar Lounge/Leisure",
"Dining: Cafeteria/Fast Food", "Dining: Family", "Dormitory",
"Exercise Center", "Exit Signs (Electroluminescent replaceing inc.)",
"Exit Signs (Electroluminescent replacing CFLs)", "Exit Signs (LED replacing CFL)",
"Exit Signs (LED replacing incandescent)", "Health Care Clinic",
"Hospital", "Hotel", "Induction Lighting replacing >=100W and <=200W HPS",
"Induction Lighting replacing >=70W and <=200W MH", "Induction Lighting replacing >200W and <=250W MH",
"Induction Lighting replacing >200W and <=400W HPS", "LED Channel Lights",
"LED traffic lights - Green 12\" - 120V", "LED traffic lights - Green 12\" - 12V",
"LED traffic lights - Green 8\" - 120V", "LED traffic lights - Green 8\" - 12V",
"LED traffic lights - Red 12\" - 120V", "LED traffic lights - Red 8\" - 120V",
"Library", "Manufacturing Facility", "Museum", "Occupancy Sensors",
"Office", "Parking Garage", "Performing Arts Theater", "Police/Fire Station",
"Retail (Other Than Mall)", "School/University", "Specialty CFL 14 w Screw-In",
"Specialty CFL 15 w Screw-In", "Specialty CFL 18 w Screw-In",
"Specialty CFL 23 w Screw-In", "Specialty CFL 27 w Screw-In",
"Specialty CFL 7 w Screw-In", "Warehouse", "Workshop", "(2) 4-lamp 4ft T5HO replacing 750W MH",
"2-lamp 4ft T8 replacing 150W MH", "3-lamp 4ft T8 replacing 250W MH",
"4-lamp 2ft T5HO replacing 150W MH", "LED Lamps NO Reflector",
"LED Lamps W/ Reflector", "MR-16 LED Lamps", "Pedestrian W/ countdown",
"Refrigerated Case LED Lamps NO motion Sensors", "Refrigerated Case LED Lamps W/ motion Sensors",
"Religious Building", "< 150 Tons", "<= 65 kBtu/h", "> 12 kBtu/h",
"> 7 and <= 9 kBtu/h", "> 9 and <= 12 kBtu/h", ">= 135 and < 240 kBtu/h",
">= 150 Tons", ">= 150 Tons and < 300 Tons", ">= 240 kBtu/h",
">= 300 Tons", ">= 65 and < 135 kBtu/h", "Advanced Diagnostic Tune Up (ADTU) 11-19.9 tons",
"Advanced Diagnostic Tune Up (ADTU) 20-25 tons", "Advanced Diagnostic Tune Up (ADTU) 4-6 tons",
"Advanced Diagnostic Tune Up (ADTU) 6-10.9 tons", "Outside Air Economizers",
"Phase I", "Phase II - Duct Seal", "Programmable Thermostat",
"Duct Test and Repair (DTR) 11-19.9 tons", "Duct Test and Repair (DTR) 2-6 tons",
"Duct Test and Repair (DTR) 6-10.9 tons", "High Performance Glazing",
"Shade Screen", ">150 HP", "1 HP", "1.5 HP", "10 HP", "100 HP",
"125 HP", "15 HP", "150 HP", "2 HP", "20 HP", "25 HP", "3 HP",
"30 HP", "40 HP", "5 HP", "50 HP", "60 HP", "7.5 HP", "75 HP",
"Anti-Sweat Heater Controls", "Beverage Machine Controls", "HiE Compressor - Walk In Cooler",
"HiE Ice Makers - Air-Cooled - 1001 to 1500lbs", "HiE Ice Makers - Air-Cooled - 101 to 200lbs",
"HiE Ice Makers - Air-Cooled - 201 to 300lbs", "HiE Ice Makers - Air-Cooled - 301 to 400lbs",
"HiE Ice Makers - Air-Cooled - 401 to 500lbs", "HiE Ice Makers - Air-Cooled - 501 to 1000lbs",
"HiE Ice Makers - Water-Cooled - 1001 to 2000lbs", "HiE Ice Makers - Water-Cooled - 101 to 200lbs",
"HiE Ice Makers - Water-Cooled - 401 to 500lbs", "HiE Ice Makers - Water-Cooled - 501 to 1000lbs",
"High-Efficiency Evaporator Fan Motors (EC)", "High-Efficiency Evaporator Fan Motors (PSC)",
"High-Efficiency Freezer (1 Door)", "High-Efficiency Freezer (2 Door)",
"High-Efficiency Freezer (3 Door)", "High-Efficiency Refrigerator (1 Door)",
"High-Efficiency Refrigerator (2 Door)", "High-Efficiency Refrigerator (3 Door)",
"Night Covers", "Snack Machine Controls", "Strip Curtains on Walk-Ins",
"Computer Power Management - Desktop", "Floating Head Controls",
"HiE Condenser", "Reach-in Cooler Controls", "CO Sensor / on-off / Office",
"CO Sensor / VAV / Office", "K-12 School", "Retail", "Dual Technology",
"Electric or Pneumatic T-Stat", "Existing EMS System", "Integrated Lighting Control",
"Misc"), class = "factor"), ee.eer = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), ee.iplv = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), sf.per.tstat = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), kwh.per.sf = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), ee.kw.per.sf = c(0.0004788, 0.00162, 0.0011559,
0.000605, 0.0008772, 0.0010797, 0.0010797, 0.0010797, 0.001248,
0.0014988, 0.00121, 0.0010308, 0.0009654, 0.0009654, 0.0011123,
0.0008511, 0.0011237, 0.0010797, 0.0014532, 0.0011559, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), ee.kwh.per.sf = c(1.63457252, 6.0314408, 4.60737506,
2.2868612, 3.57474668, 4.28859278, 4.28859278, 4.28859278, 4.8879738,
6.04189532, 4.736534, 4.30772732, 3.81041936, 3.81041936, 4.27583642,
3.33224594, 4.65193678, 4.28859278, 6.32342408, 4.60737506, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), efficiency = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NaN, 0.95, NaN, NaN,
NaN, NaN, NaN, 0.95, NaN, NaN, 0.95, NaN, NaN, 0.95, NaN, 0.95,
0.95, NaN, NaN, 0.95), motor.eff = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.949611437971035,
0.95, 0.949611437971035, 0.949611437971035, 0.949611437971035,
0.949611437971035, 0.949611437971035, 0.95, 0.949611437971035,
0.949611437971035, 0.95, 0.949611437971035, 0.949611437971035,
0.95, 0.949611437971035, 0.95, 0.95, 0.949611437971035, 0.949611437971035,
0.95), ee.lpd = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), inc.cost = c(3.177014, 0.889816, 1.502042, 1.041064, 2.436068,
1.373322, 1.373322, 1.373322, 1.1907, 2.081282, 1.12634, 2.693508,
1.180242, 1.180242, 0.493198, 0.987162, 2.382166, 1.373322, 3.401468,
1.502042, 117.631768103245, 117.631768103245, 117.631768103245,
117.631768103245, 117.631768103245, 117.631768103245, 117.631768103245,
117.631768103245, 117.631768103245, 117.631768103245, 117.631768103245,
117.631768103245, 117.631768103245, 117.631768103245, 117.631768103245,
117.631768103245, 117.631768103245, 117.631768103245, 117.631768103245,
117.631768103245), unit.qty = c(817, 5051, 836, 4027, 173610,
5328, 5668, 150, 33735.4, 3879.5, 69.7, 1356.03, 1832, 2748,
1722.9, 1850, 224, 819, 3934, 9351, 900, 1246, 265, 308, 500,
335, 550, 433, 676, 800, 350, 750, 200, 1200, 600, 433, 500,
600, 4988, 250)), .Names = c("measure", "ee.eer", "ee.iplv",
"sf.per.tstat", "kwh.per.sf", "ee.kw.per.sf", "ee.kwh.per.sf",
"efficiency", "motor.eff", "ee.lpd", "inc.cost", "unit.qty"), row.names = c(9813L,
9814L, 9815L, 9816L, 9817L, 9818L, 9819L, 9820L, 9821L, 9822L,
9823L, 9824L, 9825L, 9826L, 9827L, 9828L, 9829L, 9830L, 9831L,
9832L, 10329L, 10330L, 10331L, 10332L, 10333L, 10334L, 10335L,
10336L, 10337L, 10338L, 10339L, 10340L, 10341L, 10342L, 10343L,
10344L, 10345L, 10346L, 10347L, 10348L), class = "data.frame")
これが、Didzis Elferts の回答に基づく最終的なコードです。入力していただきありがとうございます!
j.parms.by.meas <- ddply(j.subgroups,
.(measure),
function(x) {
apply(x[c('ee.eer', 'ee.iplv', 'sf.per.tstat',
'kwh.per.sf', 'ee.kw.per.sf',
'efficiency', 'motor.eff', 'ee.lpd',
'inc.cost')],
2,weighted.mean, x$unit.qty, na.rm=T)
})