McClatchie, Sam (PIRSA-SARDI)
2005-May-30 02:48 UTC
[R] sapply following using by with a list of factors
Background: OS: Linux Mandrake 10.1 release: R 2.0.0 editor: GNU Emacs 21.3.2 front-end: ESS 5.2.3 --------------------------------- Colleagues I am having some trouble extracting results from the function by, used to average variables in a data.frame first by one factor (depth) and then by a second factor (station). The real data.frame is quite large> dim(data.2001)[1] 32049 11 Here is a snippet of code: ## bin density data for each station into 1 m depth bins, containing means data.2001.test$integer.Depth <- as.factor(round(data.2001.test$Depth, digits=0)) attach(data.2001.test) binned.data.2001 <- by(data.2001.test[,5:11], list(depth=integer.Depth, station=Station), mean) and here is a snippet of the data.frame> dim(data.2001.test)[1] 150 11> dump("data.2001.test", file=stdout())data.2001.test <- structure(list(Cruise = structure(as.integer(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Label "Ngerin01", class = "factor"), Station = structure(as.integer(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5)), .Label = c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "d1", "d2", "d3", "d4", "d5", "d6", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "g1", "g10", "g11", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "gsvc1", "gsvc2", "gsvc3", "gsvc4", "gsvc5", "gsvc6", "gsvc7", "gsvc8", "gsvc9", "gsvd1", "gsvd2", "gsvd3", "gsvd4", "gsvd5", "gsvd6", "gsvd7", "gsvd8", "h1", "h11", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "i1", "i10", "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9", "j1", "j10", "j2", "j3", "j4", "j5", "j6", "j7", "j8", "j9", "k1", "k10", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9", "l1", "l10", "l11", "l12", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9", "m1", "m10", "m11", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "mx", "n1", "n10", "n11", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "nx", "o1", "o10", "o11", "o13", "o2", "o3", "o4", "o5", "o6", "o7", "o8", "o9", "ox", "p10", "p11", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "px", "q1", "q10", "q11", "q12", "q13", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "qx", "r1", "r10", "r11", "r12", "r13", "r14", "r15", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "rx", "s1", "s10", "s11", "s12", "s13", "s14", "s15", "s16", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "sgc1", "sgc2", "sgc3", "sgc4", "sgc5", "sgc6", "sgc7", "sx", "t1", "t10", "t11", "t12", "t13", "t14", "t15", "t16", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "tx", "u1", "u10", "u11", "u12", "u13", "u14", "u15", "u2", "u3", "u4", "u5", "u6", "u7", "u8", "u9", "ux", "v1", "v10", "v11", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "vx", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "wx", "x2", "x3", "x4", "x5", "x6", "x7", "x8"), class = "factor"), Lon = c(138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.201, 138.201 ), Lat = c(-35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.95, -35.95), Depth = c(5.092, 5.289, 5.584, 5.78, 6.075, 6.37, 6.665, 7.156, 7.451, 7.845, 8.238, 8.533, 8.729, 8.926, 9.123, 9.319, 9.614, 10.106, 10.499, 10.991, 11.58, 12.17, 12.76, 13.35, 13.94, 14.53, 15.119, 15.709, 16.201, 16.594, 16.987, 17.282, 17.577, 18.069, 18.364, 18.659, 18.954, 19.15, 19.347, 19.543, 19.838, 20.232, 20.723, 21.215, 21.706, 22.394, 22.788, 23.082, 23.377, 23.574, 23.869, 24.262, 24.557, 24.95, 5.19, 5.584, 6.075, 6.567, 7.058, 7.55, 7.845, 8.14, 8.336, 8.729, 9.024, 9.516, 10.007, 10.597, 11.089, 11.777, 12.367, 12.858, 13.252, 13.645, 14.038, 14.333, 14.726, 15.021, 15.414, 16.004, 16.397, 16.987, 17.381, 17.872, 18.167, 18.462, 18.954, 19.445, 19.838, 20.428, 20.92, 21.51, 22.099, 22.591, 23.181, 23.574, 23.869, 23.869, 23.869, 23.967, 23.967, 24.066, 24.066, 24.262, 24.262, 24.36, 24.36, 5.387, 5.78, 5.977, 6.173, 6.468, 6.763, 7.156, 7.55, 8.041, 8.434, 9.024, 9.418, 9.811, 10.007, 10.302, 10.597, 10.991, 11.482, 11.875, 12.367, 12.858, 13.252, 13.645, 13.94, 14.333, 14.726, 15.218, 15.808, 16.397, 16.987, 17.479, 18.069, 18.659, 19.445, 19.937, 20.723, 21.215, 21.804, 22.198, 22.591, 22.984, 5.485, 5.977), Temperature.oC = c(19.743, 19.7421, 19.7396, 19.7354, 19.7162, 19.6801, 19.6181, 19.5434, 19.403, 19.2851, 19.2514, 19.2278, 19.2303, 19.2379, 19.248, 19.2227, 19.205, 19.2, 19.1941, 19.1907, 19.1856, 19.1772, 19.1662, 19.1561, 19.1493, 19.1434, 19.1383, 19.1341, 19.1282, 19.1172, 19.1096, 19.1113, 19.1189, 19.1451, 19.2152, 19.2641, 19.259, 19.2405, 19.2278, 19.2244, 19.2017, 19.1772, 19.1527, 19.0868, 18.6986, 18.2519, 17.9853, 17.871, 17.8237, 17.8366, 17.7143, 17.4717, 17.36, 17.3071, 19.6151, 19.6135, 19.6126, 19.6134, 19.6126, 19.6118, 19.6118, 19.6109, 19.6109, 19.6109, 19.6109, 19.6109, 19.6101, 19.6084, 19.6092, 19.61, 19.6092, 19.6075, 19.605, 19.5974, 19.5916, 19.5832, 19.5765, 19.5706, 19.5597, 19.5328, 19.5042, 19.4084, 19.2998, 19.1665, 18.9958, 18.8331, 18.6955, 18.6207, 18.5594, 18.4802, 18.4008, 18.3214, 18.2265, 18.1631, 18.1426, 18.1126, 18.1049, 18.062, 18.098, 18.11, 18.0286, 18.1117, 18.0989, 18.0989, 18.0114, 18.1152, 17.9513, 19.3769, 19.371, 19.3718, 19.3685, 19.344, 19.3137, 19.2269, 19.1036, 19.0013, 18.9488, 18.9082, 18.8573, 18.8268, 18.8157, 18.8124, 18.7996, 18.7674, 18.7198, 18.679, 18.6535, 18.6416, 18.6305, 18.611, 18.5922, 18.5514, 18.5139, 18.4934, 18.4841, 18.4968, 18.49, 18.4644, 18.4534, 18.4465, 18.4414, 18.4389, 18.4295, 18.4346, 18.4337, 18.4269, 18.4064, 18.4022, 19.3895, 19.3912), Salinity c(35.166, 35.1667, 35.2001, 35.2246, 35.2577, 35.2959, 35.3443, 35.3771, 35.4354, 35.5, 35.4849, 35.5368, 35.532, 35.5466, 35.5127, 35.5236, 35.5294, 35.5402, 35.5438, 35.5451, 35.5453, 35.5417, 35.5442, 35.5473, 35.5476, 35.5484, 35.5526, 35.5453, 35.5409, 35.5409, 35.5565, 35.567, 35.5962, 35.7026, 35.7273, 35.6732, 35.6655, 35.6442, 35.6696, 35.6644, 35.66, 35.6623, 35.6751, 35.3696, 35.2176, 35.4344, 35.5435, 35.6172, 35.6572, 35.5419, 35.3845, 35.5031, 35.6039, 35.5771, 35.5487, 35.5421, 35.5584, 35.5746, 35.5923, 35.6046, 35.5229, 35.592, 35.5998, 35.626, 35.6298, 35.6309, 35.6341, 35.6432, 35.6463, 35.6466, 35.6458, 35.647, 35.649, 35.6514, 35.6496, 35.654, 35.6491, 35.6579, 35.6381, 35.645, 35.5544, 35.5725, 35.5076, 35.4342, 35.4509, 35.4715, 35.5495, 35.5413, 35.582, 35.5519, 35.5219, 35.4919, 35.5158, 35.5611, 35.553, 35.5694, 35.5706, 35.5304, 35.5371, 35.5565, 35.5527, 35.5686, 35.5947, 35.5688, 35.5104, 35.5478, 35.5164, 35.4598, 35.5322, 35.5115, 35.5196, 35.5457, 35.598, 35.651, 35.6238, 35.5903, 35.5912, 35.5766, 35.5761, 35.5781, 35.5903, 35.5944, 35.5731, 35.5405, 35.5652, 35.5627, 35.5872, 35.5879, 35.5772, 35.5738, 35.5589, 35.5497, 35.5779, 35.5766, 35.6047, 35.5866, 35.5559, 35.5738, 35.5778, 35.5808, 35.585, 35.5708, 35.5894, 35.5848, 35.5732, 35.5587, 35.5776, 35.5798, 35.5151, 35.5029), Fluoresence.Volts = c(0.6947, 0.6789, 0.6923, 0.6935, 0.6996, 0.7045, 0.6825, 0.6911, 0.6886, 0.685, 0.6801, 0.6874, 0.7009, 0.6996, 0.6935, 0.6899, 0.7045, 0.7106, 0.7082, 0.7021, 0.7009, 0.7375, 0.7302, 0.7253, 0.7436, 0.7692, 0.7741, 0.7814, 0.7619, 0.7851, 0.7961, 0.7998, 0.7851, 0.7863, 0.7998, 0.823, 0.8388, 0.8462, 0.8718, 0.8864, 0.895, 0.873, 0.8632, 0.9035, 0.9744, 1.0049, 1.0537, 1.1026, 1.1258, 1.1258, 1.1013, 1.0952, 1.072, 1.0488, 0.7265, 0.7241, 0.7143, 0.7326, 0.7497, 0.7546, 0.7582, 0.7546, 0.7741, 0.7766, 0.7741, 0.7595, 0.7546, 0.7827, 0.7839, 0.7729, 0.7705, 0.7985, 0.8071, 0.8254, 0.8144, 0.8144, 0.8181, 0.8217, 0.8083, 0.8339, 0.8376, 0.8437, 0.8791, 0.8913, 0.9096, 0.9133, 0.9267, 0.928, 0.9158, 0.9609, 0.956, 0.9328, 0.9292, 0.9365, 0.9304, 0.9365, 0.9121, 0.917, 0.9158, 0.9109, 0.9206, 0.9048, 0.9096, 0.917, 0.9109, 0.9206, 0.9243, 0.7717, 0.7839, 0.7656, 0.7643, 0.801, 0.8217, 0.8352, 0.8523, 0.8852, 0.8864, 0.8889, 0.8706, 0.8877, 0.8913, 0.8864, 0.8901, 0.8864, 0.8962, 0.8938, 0.8901, 0.8803, 0.8999, 0.8974, 0.895, 0.8828, 0.8645, 0.884, 0.8889, 0.873, 0.8742, 0.8828, 0.8828, 0.8926, 0.8706, 0.8816, 0.8742, 0.8742, 0.8669, 0.8742, 0.8791, 0.8755, 0.7411, 0.7387), Density.kg.m3 = c(24.9796, 24.9812, 25.0087, 25.0293, 25.0609, 25.1008, 25.1553, 25.202, 25.2843, 25.3659, 25.3648, 25.4118, 25.4084, 25.4184, 25.3908, 25.4065, 25.4168, 25.4285, 25.4345, 25.4386, 25.4426, 25.4446, 25.452, 25.4595, 25.4641, 25.4689, 25.476, 25.4741, 25.4744, 25.479, 25.4946, 25.5035, 25.5251, 25.6019, 25.604, 25.5513, 25.548, 25.5374, 25.561, 25.5587, 25.5625, 25.5723, 25.5906, 25.3762, 25.3611, 25.6424, 25.794, 25.88, 25.9236, 25.8329, 25.7436, 25.8955, 26.0012, 25.9952, 25.3056, 25.3027, 25.3176, 25.3319, 25.3478, 25.3596, 25.2985, 25.3528, 25.3596, 25.3813, 25.3855, 25.3885, 25.3934, 25.4034, 25.4077, 25.4107, 25.4129, 25.4164, 25.4204, 25.4259, 25.4278, 25.4346, 25.4344, 25.4439, 25.4334, 25.4483, 25.3883, 25.4296, 25.4099, 25.3904, 25.4483, 25.5069, 25.6037, 25.6185, 25.6669, 25.6664, 25.6656, 25.6651, 25.7098, 25.7624, 25.7639, 25.7857, 25.7898, 25.7697, 25.7659, 25.7782, 25.7955, 25.7875, 25.8107, 25.7917, 25.7687, 25.772, 25.7886, 25.3006, 25.3591, 25.344, 25.3519, 25.3795, 25.4286, 25.4932, 25.506, 25.5088, 25.5246, 25.5264, 25.5408, 25.5518, 25.5648, 25.5701, 25.5584, 25.5434, 25.5765, 25.5867, 25.6141, 25.6198, 25.6161, 25.6202, 25.6149, 25.6199, 25.6526, 25.659, 25.6854, 25.671, 25.6518, 25.6741, 25.6826, 25.6892, 25.6972, 25.6891, 25.7092, 25.7065, 25.7005, 25.6928, 25.7142, 25.7187, 25.34, 25.3324), Brunt.Vaisala.Freq.cycl.h = c(15.222, 6.59496, 17.1348, 17.8131, 18.7135, 21.4038, 22.0582, 22.8786, 28.2346, 14.8181, 5.59839, 11.962, -5.12458, 7.09748, -16.0581, 1.9386, 10.7242, 7.05743, 5.22594, 3.27465, 1.23835, 0.574964, 5.06922, 4.53486, 3.30098, 3.8572, 1.97145, -4.39978, -1.18065, 5.75535, 10.227, 9.24193, 14.7518, 16.7049, 2.46144, -23.7603, -6.95984, -12.1827, 10.4526, 0.0706585, 4.84537, 7.81757, 1.99304, -32.6947, -1.00587, 35.229, 34.2314, 30.4428, 22.2066, -17.7357, -32.0899, 34.5392, 33.4394, -1.37745, -6.26124, -0.815708, 8.91883, 9.02382, 8.76263, -8.16761, -11.1727, 18.9067, 15.0293, 9.97694, 4.92582, 2.9718, 4.79608, 5.26987, 2.84495, -0.264973, 0.20777, 3.36723, 4.50213, 4.51221, 2.79895, 6.4508, -0.770261, 7.62994, 2.5534, -2.43617, -17.015, 5.25272, -12.8585, -1.89404, 24.5016, 24.5592, 21.4044, 11.1924, 7.70434, -4.09892, -4.29682, 0.10429, 15.62, 15.2757, 1.12576, 12.3537, 6.28031, 6.28031, 6.28031, 9.70625, 9.70625, 16.6939, 16.6939, -16.9003, -16.9003, -16.5469, -16.5469, -1.24881, 2.12382, -8.88365, 11.8552, 18.6224, 22.4818, 17.7073, 7.05875, 5.60178, 7.24009, 3.48756, 9.56937, 10.7906, 13.3156, 2.31384, -11.7071, -5.06014, 11.9814, 9.25461, 10.2528, 1.04765, -3.92472, 0.97033, -6.84208, 7.41494, 13.7583, 6.60244, 5.18133, -9.85804, -4.08612, 10.1503, 5.32709, 4.55223, -0.413365, -5.7495, 3.18065, -5.70216, -7.24117, -7.31205, 12.0855, 4.33021, -4.98771, -2.93681), integer.Depth = c(5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 11, 12, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 5, 6)), .Names = c("Cruise", "Station", "Lon", "Lat", "Depth", "Temperature.oC", "Salinity", "Fluoresence.Volts", "Density.kg.m3", "Brunt.Vaisala.Freq.cycl.h", "integer.Depth" ), row.names = c("19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "209", "210"), class = "data.frame") When I run this code on the full dataset, calculations continue long enough to suggest I am generating a huge matrix, so perhaps I'm doing something silly? Eventually (well, after maybe 5 minutes) I get a by class object of 109 rows (depth category) by 288 columns (station category), so it does seem to be working. I know that you use sapply to get the by class back to a data.frame. I want to extract a matrix of mean densities (one of the original variables) at each of the 109 depths and 288 stations. I have not quite got this right...can you help? Thanks in advance Sam ---- Sam McClatchie, Biological oceanography South Australian Aquatic Sciences Centre PO Box 120, Henley Beach 5022 Adelaide, South Australia email <mcclatchie.sam at saugov.sa.gov.au> Telephone: (61-8) 8207 5448 FAX: (61-8) 8207 5481 Research home page <http://www.members.iinet.net.au/~s.mcclatchie/> /\ ...>><xX(??> //// \\\\ <??)Xx><< ///// \\\\\\ ><(((??> >><(((??> ...>><xX(??>O<??)Xx><<
Gabor Grothendieck
2005-May-30 03:42 UTC
[R] sapply following using by with a list of factors
On 5/29/05, McClatchie, Sam (PIRSA-SARDI) <mcclatchie.sam at saugov.sa.gov.au> wrote:> Background: > OS: Linux Mandrake 10.1 > release: R 2.0.0 > editor: GNU Emacs 21.3.2 > front-end: ESS 5.2.3 > --------------------------------- > Colleagues > > I am having some trouble extracting results from the function by, used to > average variables in a data.frame first by one factor (depth) and then by a > second factor (station). The real data.frame is quite large > > dim(data.2001) > [1] 32049 11 > > Here is a snippet of code: > > ## bin density data for each station into 1 m depth bins, containing means > data.2001.test$integer.Depth <- as.factor(round(data.2001.test$Depth, > digits=0)) > attach(data.2001.test) > binned.data.2001 <- by(data.2001.test[,5:11], list(depth=integer.Depth, > station=Station), mean) > > and here is a snippet of the data.frame > > > dim(data.2001.test) > [1] 150 11 > > dump("data.2001.test", file=stdout()) > data.2001.test <- > structure(list(Cruise = structure(as.integer(c(1, 1, 1, 1, 1,Try the following. To keep this short lets just take a subset of rows called dd. Also, we drop the Station levels that are not being used since this test only uses 2 levels and there are 288 Station levels in total. The function that we apply using by returns a vector consisting of the integer.Depth, Station and the column means of columns 5 to 10. (Asking for just the mean of those, as in your example, would take all the numbers in all the columns passed to mean and give back a grand mean rather than a mean per column.) Finally we rbind it all back together.> # data.2001.test is your data frame including the integer.Depth column > dd <- data.2001.test[50:60,] > dd$Station <- dd$Station[drop = TRUE] > dd.bin <- by(dd, list(dd$integer.Depth, dd$Station), function(x)+ c(integer.Depth = x$integer.Depth[1], Station = x$Station[1], + colMeans(x[,5:10])))> do.call("rbind", dd.bin)integer.Depth Station Depth Temperature.oC Salinity Fluoresence.Volts [1,] 20 1 23.90167 17.67420 35.47650 1.107433 [2,] 21 1 24.75350 17.33355 35.59050 1.060400 [3,] 1 2 5.19000 19.61510 35.54870 0.726500 [4,] 2 2 5.82950 19.61305 35.55025 0.719200 [5,] 3 2 6.81250 19.61300 35.58345 0.741150 [6,] 4 2 7.55000 19.61180 35.60460 0.754600 Density.kg.m3 Brunt.Vaisala.Freq.cycl.h [1,] 25.82400 -5.095467 [2,] 25.99820 16.030975 [3,] 25.30560 -6.261240 [4,] 25.31015 4.051561 [5,] 25.33985 8.893225 [6,] 25.35960 -8.167610
Gabor Grothendieck
2005-May-30 03:58 UTC
[R] sapply following using by with a list of factors
On 5/29/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On 5/29/05, McClatchie, Sam (PIRSA-SARDI) > <mcclatchie.sam at saugov.sa.gov.au> wrote: > > Background: > > OS: Linux Mandrake 10.1 > > release: R 2.0.0 > > editor: GNU Emacs 21.3.2 > > front-end: ESS 5.2.3 > > --------------------------------- > > Colleagues > > > > I am having some trouble extracting results from the function by, used to > > average variables in a data.frame first by one factor (depth) and then by a > > second factor (station). The real data.frame is quite large > > > dim(data.2001) > > [1] 32049 11 > > > > Here is a snippet of code: > > > > ## bin density data for each station into 1 m depth bins, containing means > > data.2001.test$integer.Depth <- as.factor(round(data.2001.test$Depth, > > digits=0)) > > attach(data.2001.test) > > binned.data.2001 <- by(data.2001.test[,5:11], list(depth=integer.Depth, > > station=Station), mean) > > > > and here is a snippet of the data.frame > > > > > dim(data.2001.test) > > [1] 150 11 > > > dump("data.2001.test", file=stdout()) > > data.2001.test <- > > structure(list(Cruise = structure(as.integer(c(1, 1, 1, 1, 1, > > > Try the following. To keep this short lets just take a subset > of rows called dd. Also, we drop the Station levels > that are not being used since this test only uses 2 levels > and there are 288 Station levels in total. The function that we apply using > by returns a vector consisting of the integer.Depth, Station > and the column means of columns 5 to 10. (Asking for just the > mean of those, as in your example, would take all the numbers > in all the columns passed to mean and give back a grand mean > rather than a mean per column.) Finally we rbind it all back together. > > > # data.2001.test is your data frame including the integer.Depth column > > dd <- data.2001.test[50:60,] > > dd$Station <- dd$Station[drop = TRUE] > > dd.bin <- by(dd, list(dd$integer.Depth, dd$Station), function(x) > + c(integer.Depth = x$integer.Depth[1], Station = x$Station[1], > + colMeans(x[,5:10]))) > > do.call("rbind", dd.bin) > integer.Depth Station Depth Temperature.oC Salinity Fluoresence.Volts > [1,] 20 1 23.90167 17.67420 35.47650 1.107433 > [2,] 21 1 24.75350 17.33355 35.59050 1.060400 > [3,] 1 2 5.19000 19.61510 35.54870 0.726500 > [4,] 2 2 5.82950 19.61305 35.55025 0.719200 > [5,] 3 2 6.81250 19.61300 35.58345 0.741150 > [6,] 4 2 7.55000 19.61180 35.60460 0.754600 > Density.kg.m3 Brunt.Vaisala.Freq.cycl.h > [1,] 25.82400 -5.095467 > [2,] 25.99820 16.030975 > [3,] 25.30560 -6.261240 > [4,] 25.31015 4.051561 > [5,] 25.33985 8.893225 > [6,] 25.35960 -8.167610 >Here is a correction for the fact that the first two columns are factors. This time, instead of creating a vector in the function we create a one row data frame.> # previous lines as above > dd.bin <- by(dd, list(dd$integer.Depth, dd$Station), function(x)+ cbind(data.frame(integer.Depth = x$integer.Depth[1], + Station = x$Station[1]), t(colMeans(x[,5:10]))))> do.call("rbind", dd.bin)integer.Depth Station Depth Temperature.oC Salinity Fluoresence.Volts 1 24 a2 23.90167 17.67420 35.47650 1.107433 11 25 a2 24.75350 17.33355 35.59050 1.060400 12 5 a3 5.19000 19.61510 35.54870 0.726500 13 6 a3 5.82950 19.61305 35.55025 0.719200 14 7 a3 6.81250 19.61300 35.58345 0.741150 15 8 a3 7.55000 19.61180 35.60460 0.754600 Density.kg.m3 Brunt.Vaisala.Freq.cycl.h 1 25.82400 -5.095467 11 25.99820 16.030975 12 25.30560 -6.261240 13 25.31015 4.051561 14 25.33985 8.893225 15 25.35960 -8.167610
Frank E Harrell Jr
2005-May-30 11:34 UTC
[R] sapply following using by with a list of factors
McClatchie, Sam (PIRSA-SARDI) wrote:> Background: > OS: Linux Mandrake 10.1 > release: R 2.0.0 > editor: GNU Emacs 21.3.2 > front-end: ESS 5.2.3 > --------------------------------- > Colleagues > > I am having some trouble extracting results from the function by, used to > average variables in a data.frame first by one factor (depth) and then by a > second factor (station). The real data.frame is quite large > >>dim(data.2001) > > [1] 32049 11 > > Here is a snippet of code: > > ## bin density data for each station into 1 m depth bins, containing means > data.2001.test$integer.Depth <- as.factor(round(data.2001.test$Depth, > digits=0)) > attach(data.2001.test) > binned.data.2001 <- by(data.2001.test[,5:11], list(depth=integer.Depth, > station=Station), mean) > > and here is a snippet of the data.frame > > >>dim(data.2001.test) > > [1] 150 11 > >>dump("data.2001.test", file=stdout()) > > data.2001.test <- > structure(list(Cruise = structure(as.integer(c(1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Label > "Ngerin01", class = "factor"), > Station = structure(as.integer(c(2, 2, 2, 2, 2, 2, 2, 2, > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, > 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, > 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, > 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, > 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, > 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, > 4, 4, 4, 4, 4, 4, 4, 5, 5)), .Label = c("a1", "a2", "a3", > "a4", "a5", "a6", "a7", "a8", "a9", "b1", "b2", "b3", "b4", > "b5", "b6", "b7", "c1", "c2", "c3", "c4", "c5", "c6", "c7", > "d1", "d2", "d3", "d4", "d5", "d6", "e1", "e2", "e3", "e4", > "e5", "e6", "e7", "f1", "f2", "f3", "f4", "f5", "f6", "f7", > "f8", "f9", "g1", "g10", "g11", "g2", "g3", "g4", "g5", "g6", > "g7", "g8", "g9", "gsvc1", "gsvc2", "gsvc3", "gsvc4", "gsvc5", > "gsvc6", "gsvc7", "gsvc8", "gsvc9", "gsvd1", "gsvd2", "gsvd3", > "gsvd4", "gsvd5", "gsvd6", "gsvd7", "gsvd8", "h1", "h11", > "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "i1", "i10", > "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9", "j1", "j10", > "j2", "j3", "j4", "j5", "j6", "j7", "j8", "j9", "k1", "k10", > "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9", "l1", "l10", > "l11", "l12", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9", > "m1", "m10", "m11", "m2", "m3", "m4", "m5", "m6", "m7", "m8", > "m9", "mx", "n1", "n10", "n11", "n2", "n3", "n4", "n5", "n6", > "n7", "n8", "n9", "nx", "o1", "o10", "o11", "o13", "o2", > "o3", "o4", "o5", "o6", "o7", "o8", "o9", "ox", "p10", "p11", > "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "px", "q1", > "q10", "q11", "q12", "q13", "q2", "q3", "q4", "q5", "q6", > "q7", "q8", "q9", "qx", "r1", "r10", "r11", "r12", "r13", > "r14", "r15", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", > "rx", "s1", "s10", "s11", "s12", "s13", "s14", "s15", "s16", > "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "sgc1", "sgc2", > "sgc3", "sgc4", "sgc5", "sgc6", "sgc7", "sx", "t1", "t10", > "t11", "t12", "t13", "t14", "t15", "t16", "t2", "t3", "t4", > "t5", "t6", "t7", "t8", "t9", "tx", "u1", "u10", "u11", "u12", > "u13", "u14", "u15", "u2", "u3", "u4", "u5", "u6", "u7", > "u8", "u9", "ux", "v1", "v10", "v11", "v2", "v3", "v4", "v5", > "v6", "v7", "v8", "v9", "vx", "w2", "w3", "w4", "w5", "w6", > "w7", "w8", "w9", "wx", "x2", "x3", "x4", "x5", "x6", "x7", > "x8"), class = "factor"), Lon = c(138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, 138.421, > 138.421, 138.421, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, > 138.352, 138.352, 138.352, 138.352, 138.352, 138.352, 138.277, > 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, > 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, > 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, > 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, > 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, 138.277, > 138.277, 138.277, 138.277, 138.277, 138.277, 138.201, 138.201 > ), Lat = c(-35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, > -35.766, -35.766, -35.766, -35.766, -35.766, -35.766, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, -35.827, > -35.827, -35.827, -35.827, -35.883, -35.883, -35.883, -35.883, > -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, > -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, > -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, > -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, > -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, -35.883, > -35.883, -35.883, -35.95, -35.95), Depth = c(5.092, 5.289, > 5.584, 5.78, 6.075, 6.37, 6.665, 7.156, 7.451, 7.845, 8.238, > 8.533, 8.729, 8.926, 9.123, 9.319, 9.614, 10.106, 10.499, > 10.991, 11.58, 12.17, 12.76, 13.35, 13.94, 14.53, 15.119, > 15.709, 16.201, 16.594, 16.987, 17.282, 17.577, 18.069, 18.364, > 18.659, 18.954, 19.15, 19.347, 19.543, 19.838, 20.232, 20.723, > 21.215, 21.706, 22.394, 22.788, 23.082, 23.377, 23.574, 23.869, > 24.262, 24.557, 24.95, 5.19, 5.584, 6.075, 6.567, 7.058, > 7.55, 7.845, 8.14, 8.336, 8.729, 9.024, 9.516, 10.007, 10.597, > 11.089, 11.777, 12.367, 12.858, 13.252, 13.645, 14.038, 14.333, > 14.726, 15.021, 15.414, 16.004, 16.397, 16.987, 17.381, 17.872, > 18.167, 18.462, 18.954, 19.445, 19.838, 20.428, 20.92, 21.51, > 22.099, 22.591, 23.181, 23.574, 23.869, 23.869, 23.869, 23.967, > 23.967, 24.066, 24.066, 24.262, 24.262, 24.36, 24.36, 5.387, > 5.78, 5.977, 6.173, 6.468, 6.763, 7.156, 7.55, 8.041, 8.434, > 9.024, 9.418, 9.811, 10.007, 10.302, 10.597, 10.991, 11.482, > 11.875, 12.367, 12.858, 13.252, 13.645, 13.94, 14.333, 14.726, > 15.218, 15.808, 16.397, 16.987, 17.479, 18.069, 18.659, 19.445, > 19.937, 20.723, 21.215, 21.804, 22.198, 22.591, 22.984, 5.485, > 5.977), Temperature.oC = c(19.743, 19.7421, 19.7396, 19.7354, > 19.7162, 19.6801, 19.6181, 19.5434, 19.403, 19.2851, 19.2514, > 19.2278, 19.2303, 19.2379, 19.248, 19.2227, 19.205, 19.2, > 19.1941, 19.1907, 19.1856, 19.1772, 19.1662, 19.1561, 19.1493, > 19.1434, 19.1383, 19.1341, 19.1282, 19.1172, 19.1096, 19.1113, > 19.1189, 19.1451, 19.2152, 19.2641, 19.259, 19.2405, 19.2278, > 19.2244, 19.2017, 19.1772, 19.1527, 19.0868, 18.6986, 18.2519, > 17.9853, 17.871, 17.8237, 17.8366, 17.7143, 17.4717, 17.36, > 17.3071, 19.6151, 19.6135, 19.6126, 19.6134, 19.6126, 19.6118, > 19.6118, 19.6109, 19.6109, 19.6109, 19.6109, 19.6109, 19.6101, > 19.6084, 19.6092, 19.61, 19.6092, 19.6075, 19.605, 19.5974, > 19.5916, 19.5832, 19.5765, 19.5706, 19.5597, 19.5328, 19.5042, > 19.4084, 19.2998, 19.1665, 18.9958, 18.8331, 18.6955, 18.6207, > 18.5594, 18.4802, 18.4008, 18.3214, 18.2265, 18.1631, 18.1426, > 18.1126, 18.1049, 18.062, 18.098, 18.11, 18.0286, 18.1117, > 18.0989, 18.0989, 18.0114, 18.1152, 17.9513, 19.3769, 19.371, > 19.3718, 19.3685, 19.344, 19.3137, 19.2269, 19.1036, 19.0013, > 18.9488, 18.9082, 18.8573, 18.8268, 18.8157, 18.8124, 18.7996, > 18.7674, 18.7198, 18.679, 18.6535, 18.6416, 18.6305, 18.611, > 18.5922, 18.5514, 18.5139, 18.4934, 18.4841, 18.4968, 18.49, > 18.4644, 18.4534, 18.4465, 18.4414, 18.4389, 18.4295, 18.4346, > 18.4337, 18.4269, 18.4064, 18.4022, 19.3895, 19.3912), Salinity > c(35.166, > 35.1667, 35.2001, 35.2246, 35.2577, 35.2959, 35.3443, 35.3771, > 35.4354, 35.5, 35.4849, 35.5368, 35.532, 35.5466, 35.5127, > 35.5236, 35.5294, 35.5402, 35.5438, 35.5451, 35.5453, 35.5417, > 35.5442, 35.5473, 35.5476, 35.5484, 35.5526, 35.5453, 35.5409, > 35.5409, 35.5565, 35.567, 35.5962, 35.7026, 35.7273, 35.6732, > 35.6655, 35.6442, 35.6696, 35.6644, 35.66, 35.6623, 35.6751, > 35.3696, 35.2176, 35.4344, 35.5435, 35.6172, 35.6572, 35.5419, > 35.3845, 35.5031, 35.6039, 35.5771, 35.5487, 35.5421, 35.5584, > 35.5746, 35.5923, 35.6046, 35.5229, 35.592, 35.5998, 35.626, > 35.6298, 35.6309, 35.6341, 35.6432, 35.6463, 35.6466, 35.6458, > 35.647, 35.649, 35.6514, 35.6496, 35.654, 35.6491, 35.6579, > 35.6381, 35.645, 35.5544, 35.5725, 35.5076, 35.4342, 35.4509, > 35.4715, 35.5495, 35.5413, 35.582, 35.5519, 35.5219, 35.4919, > 35.5158, 35.5611, 35.553, 35.5694, 35.5706, 35.5304, 35.5371, > 35.5565, 35.5527, 35.5686, 35.5947, 35.5688, 35.5104, 35.5478, > 35.5164, 35.4598, 35.5322, 35.5115, 35.5196, 35.5457, 35.598, > 35.651, 35.6238, 35.5903, 35.5912, 35.5766, 35.5761, 35.5781, > 35.5903, 35.5944, 35.5731, 35.5405, 35.5652, 35.5627, 35.5872, > 35.5879, 35.5772, 35.5738, 35.5589, 35.5497, 35.5779, 35.5766, > 35.6047, 35.5866, 35.5559, 35.5738, 35.5778, 35.5808, 35.585, > 35.5708, 35.5894, 35.5848, 35.5732, 35.5587, 35.5776, 35.5798, > 35.5151, 35.5029), Fluoresence.Volts = c(0.6947, 0.6789, > 0.6923, 0.6935, 0.6996, 0.7045, 0.6825, 0.6911, 0.6886, 0.685, > 0.6801, 0.6874, 0.7009, 0.6996, 0.6935, 0.6899, 0.7045, 0.7106, > 0.7082, 0.7021, 0.7009, 0.7375, 0.7302, 0.7253, 0.7436, 0.7692, > 0.7741, 0.7814, 0.7619, 0.7851, 0.7961, 0.7998, 0.7851, 0.7863, > 0.7998, 0.823, 0.8388, 0.8462, 0.8718, 0.8864, 0.895, 0.873, > 0.8632, 0.9035, 0.9744, 1.0049, 1.0537, 1.1026, 1.1258, 1.1258, > 1.1013, 1.0952, 1.072, 1.0488, 0.7265, 0.7241, 0.7143, 0.7326, > 0.7497, 0.7546, 0.7582, 0.7546, 0.7741, 0.7766, 0.7741, 0.7595, > 0.7546, 0.7827, 0.7839, 0.7729, 0.7705, 0.7985, 0.8071, 0.8254, > 0.8144, 0.8144, 0.8181, 0.8217, 0.8083, 0.8339, 0.8376, 0.8437, > 0.8791, 0.8913, 0.9096, 0.9133, 0.9267, 0.928, 0.9158, 0.9609, > 0.956, 0.9328, 0.9292, 0.9365, 0.9304, 0.9365, 0.9121, 0.917, > 0.9158, 0.9109, 0.9206, 0.9048, 0.9096, 0.917, 0.9109, 0.9206, > 0.9243, 0.7717, 0.7839, 0.7656, 0.7643, 0.801, 0.8217, 0.8352, > 0.8523, 0.8852, 0.8864, 0.8889, 0.8706, 0.8877, 0.8913, 0.8864, > 0.8901, 0.8864, 0.8962, 0.8938, 0.8901, 0.8803, 0.8999, 0.8974, > 0.895, 0.8828, 0.8645, 0.884, 0.8889, 0.873, 0.8742, 0.8828, > 0.8828, 0.8926, 0.8706, 0.8816, 0.8742, 0.8742, 0.8669, 0.8742, > 0.8791, 0.8755, 0.7411, 0.7387), Density.kg.m3 = c(24.9796, > 24.9812, 25.0087, 25.0293, 25.0609, 25.1008, 25.1553, 25.202, > 25.2843, 25.3659, 25.3648, 25.4118, 25.4084, 25.4184, 25.3908, > 25.4065, 25.4168, 25.4285, 25.4345, 25.4386, 25.4426, 25.4446, > 25.452, 25.4595, 25.4641, 25.4689, 25.476, 25.4741, 25.4744, > 25.479, 25.4946, 25.5035, 25.5251, 25.6019, 25.604, 25.5513, > 25.548, 25.5374, 25.561, 25.5587, 25.5625, 25.5723, 25.5906, > 25.3762, 25.3611, 25.6424, 25.794, 25.88, 25.9236, 25.8329, > 25.7436, 25.8955, 26.0012, 25.9952, 25.3056, 25.3027, 25.3176, > 25.3319, 25.3478, 25.3596, 25.2985, 25.3528, 25.3596, 25.3813, > 25.3855, 25.3885, 25.3934, 25.4034, 25.4077, 25.4107, 25.4129, > 25.4164, 25.4204, 25.4259, 25.4278, 25.4346, 25.4344, 25.4439, > 25.4334, 25.4483, 25.3883, 25.4296, 25.4099, 25.3904, 25.4483, > 25.5069, 25.6037, 25.6185, 25.6669, 25.6664, 25.6656, 25.6651, > 25.7098, 25.7624, 25.7639, 25.7857, 25.7898, 25.7697, 25.7659, > 25.7782, 25.7955, 25.7875, 25.8107, 25.7917, 25.7687, 25.772, > 25.7886, 25.3006, 25.3591, 25.344, 25.3519, 25.3795, 25.4286, > 25.4932, 25.506, 25.5088, 25.5246, 25.5264, 25.5408, 25.5518, > 25.5648, 25.5701, 25.5584, 25.5434, 25.5765, 25.5867, 25.6141, > 25.6198, 25.6161, 25.6202, 25.6149, 25.6199, 25.6526, 25.659, > 25.6854, 25.671, 25.6518, 25.6741, 25.6826, 25.6892, 25.6972, > 25.6891, 25.7092, 25.7065, 25.7005, 25.6928, 25.7142, 25.7187, > 25.34, 25.3324), Brunt.Vaisala.Freq.cycl.h = c(15.222, 6.59496, > 17.1348, 17.8131, 18.7135, 21.4038, 22.0582, 22.8786, 28.2346, > 14.8181, 5.59839, 11.962, -5.12458, 7.09748, -16.0581, 1.9386, > 10.7242, 7.05743, 5.22594, 3.27465, 1.23835, 0.574964, 5.06922, > 4.53486, 3.30098, 3.8572, 1.97145, -4.39978, -1.18065, 5.75535, > 10.227, 9.24193, 14.7518, 16.7049, 2.46144, -23.7603, -6.95984, > -12.1827, 10.4526, 0.0706585, 4.84537, 7.81757, 1.99304, > -32.6947, -1.00587, 35.229, 34.2314, 30.4428, 22.2066, -17.7357, > -32.0899, 34.5392, 33.4394, -1.37745, -6.26124, -0.815708, > 8.91883, 9.02382, 8.76263, -8.16761, -11.1727, 18.9067, 15.0293, > 9.97694, 4.92582, 2.9718, 4.79608, 5.26987, 2.84495, -0.264973, > 0.20777, 3.36723, 4.50213, 4.51221, 2.79895, 6.4508, -0.770261, > 7.62994, 2.5534, -2.43617, -17.015, 5.25272, -12.8585, -1.89404, > 24.5016, 24.5592, 21.4044, 11.1924, 7.70434, -4.09892, -4.29682, > 0.10429, 15.62, 15.2757, 1.12576, 12.3537, 6.28031, 6.28031, > 6.28031, 9.70625, 9.70625, 16.6939, 16.6939, -16.9003, -16.9003, > -16.5469, -16.5469, -1.24881, 2.12382, -8.88365, 11.8552, > 18.6224, 22.4818, 17.7073, 7.05875, 5.60178, 7.24009, 3.48756, > 9.56937, 10.7906, 13.3156, 2.31384, -11.7071, -5.06014, 11.9814, > 9.25461, 10.2528, 1.04765, -3.92472, 0.97033, -6.84208, 7.41494, > 13.7583, 6.60244, 5.18133, -9.85804, -4.08612, 10.1503, 5.32709, > 4.55223, -0.413365, -5.7495, 3.18065, -5.70216, -7.24117, > -7.31205, 12.0855, 4.33021, -4.98771, -2.93681), integer.Depth = c(5, > 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, > 11, 12, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 17, 18, 18, > 18, 19, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 23, 23, 23, > 24, 24, 24, 25, 25, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, > 10, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, > 17, 17, 18, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, > 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 5, 6, 6, 6, 6, > 7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, > 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18, 19, 19, 20, 21, > 21, 22, 22, 23, 23, 5, 6)), .Names = c("Cruise", "Station", > "Lon", "Lat", "Depth", "Temperature.oC", "Salinity", "Fluoresence.Volts", > "Density.kg.m3", "Brunt.Vaisala.Freq.cycl.h", "integer.Depth" > ), row.names = c("19", "20", "21", "22", "23", "24", "25", "26", > "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", > "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", > "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", > "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", > "71", "72", "88", "89", "90", "91", "92", "93", "94", "95", "96", > "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", > "107", "108", "109", "110", "111", "112", "113", "114", "115", > "116", "117", "118", "119", "120", "121", "122", "123", "124", > "125", "126", "127", "128", "129", "130", "131", "132", "133", > "134", "135", "136", "137", "138", "139", "140", "154", "155", > "156", "157", "158", "159", "160", "161", "162", "163", "164", > "165", "166", "167", "168", "169", "170", "171", "172", "173", > "174", "175", "176", "177", "178", "179", "180", "181", "182", > "183", "184", "185", "186", "187", "188", "189", "190", "191", > "192", "193", "194", "209", "210"), class = "data.frame") > > When I run this code on the full dataset, calculations continue long enough > to suggest I am generating a huge matrix, so perhaps I'm doing something > silly? Eventually (well, after maybe 5 minutes) I get a by class object of > 109 rows (depth category) by 288 columns (station category), so it does seem > to be working. > > I know that you use sapply to get the by class back to a data.frame. I want > to extract a matrix of mean densities (one of the original variables) at > each of the 109 depths and 288 stations. > I have not quite got this right...can you help? > > Thanks in advance > > Sam > ---- > Sam McClatchie, > Biological oceanography > South Australian Aquatic Sciences CentreYou might try the summarize function in the Hmisc package. -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University