我在zoo对象中有一个数据集。我循环遍历行,从中提取变量,并通过为每行输出数据帧的函数运行它们。然后我将这些输出数据组合成一个数据框,该数据框的行与我原来的zoo对象相匹配。我想将它们组合起来,将新列添加到原始动物园。
我的数据是 testZ
> testZ
X107b_pH X107b_SP X107b_temp Sal TApred
(06/22/12 03:00:00) 8.5124 7.32 16.16140 29.5935 2061.212
(06/22/12 04:00:00) 8.5124 7.32 15.93407 30.0677 2083.886
(06/22/12 05:00:00) 8.5124 7.32 15.76140 30.1329 2087.003
(06/22/12 06:00:00) 8.5124 7.32 15.59807 29.9522 2078.363
(06/22/12 07:00:00) 8.5124 7.32 15.46307 29.9091 2076.303
(06/22/12 08:00:00) 8.5124 7.32 15.44640 29.8565 2073.788
(06/22/12 09:00:00) 8.5124 7.32 15.44640 29.8647 2074.180
(06/22/12 10:00:00) 8.5124 7.32 15.44640 29.8568 2073.802
(06/22/12 11:00:00) 8.5124 7.32 15.47640 29.8092 2071.526
(06/22/12 12:00:00) 8.5124 7.32 15.58640 29.7966 2070.923
(06/22/12 13:00:00) 8.5124 7.32 15.60712 29.8247 2072.267
和testS(从seacarb包中的carb函数输出)
> testS
flag S T P pH CO2 pCO2 fCO2 HCO3 CO3 DIC ALK OmegaAragonite OmegaCalcite
100 8 29.5935 16.16140 0 8.5124 3.640861e-06 98.00598 97.66177 0.001327675 0.0002942433 0.001625559 0.002061212 4.618012 7.265870
101 8 30.0677 15.93407 0 8.5124 3.676374e-06 98.55170 98.20457 0.001340472 0.0002981053 0.001642254 0.002083886 4.668223 7.337512
102 8 30.1329 15.76140 0 8.5124 3.700343e-06 98.71823 98.36976 0.001344778 0.0002975873 0.001646066 0.002087003 4.656435 7.320294
103 8 29.9522 15.59807 0 8.5124 3.720354e-06 98.66681 98.31780 0.001344355 0.0002942804 0.001642355 0.002078363 4.604700 7.245624
104 8 29.9091 15.46307 0 8.5124 3.738210e-06 98.71419 98.36442 0.001345958 0.0002927887 0.001642485 0.002076303 4.579875 7.209657
105 8 29.8565 15.44640 0 8.5124 3.739859e-06 98.67915 98.32943 0.001345269 0.0002920590 0.001641068 0.002073788 4.568917 7.193862
106 8 29.8647 15.44640 0 8.5124 3.739957e-06 98.68623 98.33648 0.001345423 0.0002921536 0.001641317 0.002074180 4.570287 7.195833
107 8 29.8568 15.44640 0 8.5124 3.739863e-06 98.67941 98.32969 0.001345275 0.0002920624 0.001641077 0.002073802 4.568967 7.193934
108 8 29.8092 15.47640 0 8.5124 3.735212e-06 98.61975 98.27037 0.001343849 0.0002917330 0.001639318 0.002071526 4.564912 7.188167
109 8 29.7966 15.58640 0 8.5124 3.720121e-06 98.54050 98.19189 0.001341659 0.0002923933 0.001637773 0.002070923 4.577113 7.205942
110 8 29.8247 15.60712 0 8.5124 3.717640e-06 98.55167 98.20311 0.001341816 0.0002928703 0.001638404 0.002072267 4.584520 7.216633
我试过cbind,但它给了我一个令人费解的结果。如果我使用
testM=cbind(testZ,testS$pCO2)
它将完成我的预期,并将pCO2列添加到zoo对象testZ中。但是,如果我调用以下内容,则无效。
> testM=cbind(testZ,testS)
Warning message:
In cbind(testZ, testS) :
number of rows of result is not a multiple of vector length (arg 2)
感谢您的任何建议。我是动物园的新手,所以很可能我错过了什么。
dput()输出:
> dput(testZ)
structure(c(8.5124, 8.5124, 8.5124, 8.5124, 8.5124, 8.5124, 8.5124,
8.5124, 8.5124, 8.5124, 8.5124, 7.32, 7.32, 7.32, 7.32, 7.32,
7.32, 7.32, 7.32, 7.32, 7.32, 7.32, 16.1614, 15.9340666666667,
15.7614, 15.5980666666667, 15.4630666666667, 15.4464, 15.4464,
15.4464, 15.4764, 15.5864, 15.6071216666667, 29.5935, 30.0677,
30.1329, 29.9522, 29.9091, 29.8565, 29.8647, 29.8568, 29.8092,
29.7966, 29.8247, 2061.2124, 2083.885921, 2087.00341, 2078.363374,
2076.30258, 2073.78755, 2074.179627, 2073.801894, 2071.525935,
2070.923476, 2072.267056), .Dim = c(11L, 5L), .Dimnames = list(
NULL, c("X107b_pH", "X107b_SP", "X107b_temp", "Sal", "TApred"
)), index = structure(c(15513.125, 15513.1666666667, 15513.2083333333,
15513.25, 15513.2916666667, 15513.3333333333, 15513.375, 15513.4166666667,
15513.4583333333, 15513.5, 15513.5416666667), format = structure(c("m/d/y",
"h:m:s"), .Names = c("dates", "times")), origin = c(1, 1, 1970
), class = c("chron", "dates", "times")), class = "zoo")
> dput(testS)
structure(list(flag = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8), S = c(29.5935,
30.0677, 30.1329, 29.9522, 29.9091, 29.8565, 29.8647, 29.8568,
29.8092, 29.7966, 29.8247), T = c(16.1614, 15.9340666666667,
15.7614, 15.5980666666667, 15.4630666666667, 15.4464, 15.4464,
15.4464, 15.4764, 15.5864, 15.6071216666667), P = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), pH = c(8.5124, 8.5124, 8.5124, 8.5124,
8.5124, 8.5124, 8.5124, 8.5124, 8.5124, 8.5124, 8.5124), CO2 = c(3.64086135363789e-06,
3.67637421195346e-06, 3.70034276494183e-06, 3.72035392339414e-06,
3.73820956050126e-06, 3.73985945475865e-06, 3.73995661519692e-06,
3.73986300857744e-06, 3.73521210447936e-06, 3.7201209120172e-06,
3.71764026477019e-06), pCO2 = c(98.005982410781, 98.551699365364,
98.7182334593185, 98.6668114430222, 98.7141900061466, 98.6791533613055,
98.6862308435365, 98.6794122638734, 98.6197523510141, 98.5405045633332,
98.5516674237132), fCO2 = c(97.6617674560379, 98.2045735556704,
98.3697621203092, 98.3178020344274, 98.3644164625271, 98.3294302472383,
98.3364826465715, 98.3296882322446, 98.270372350004, 98.1918905249818,
98.2031052068924), HCO3 = c(0.00132767523331992, 0.00134047197543355,
0.00134477847686806, 0.00134435458599512, 0.00134595801586938,
0.00134526939095695, 0.00134542309836435, 0.00134527501465186,
0.00134384946047717, 0.00134165916833029, 0.00134181561030948
), CO3 = c(0.0002942433395289, 0.000298105334561092, 0.000297587342867106,
0.000294280388575961, 0.000292788714950415, 0.000292058968639722,
0.000292153613334272, 0.000292062431011073, 0.000291732984210706,
0.000292393341741146, 0.000292870295328662), DIC = c(0.00162555943420246,
0.0016422536842066, 0.00164606616250011, 0.00164235532849448,
0.0016424849403803, 0.00164106821905143, 0.00164131666831381,
0.00164107730867151, 0.00163931765679236, 0.00163777263098345,
0.00163840354590291), ALK = c(0.0020612124, 0.002083885921, 0.00208700341,
0.002078363374, 0.00207630258, 0.00207378755, 0.002074179627,
0.002073801894, 0.002071525935, 0.002070923476, 0.002072267056
), OmegaAragonite = c(4.61801172079652, 4.66822300723876, 4.6564345684589,
4.60469962461282, 4.57987479287612, 4.56891702268792, 4.5702869874125,
4.56896714300747, 4.56491213342598, 4.57711290054851, 4.58452034914768
), OmegaCalcite = c(7.2658700466138, 7.33751216404482, 7.32029446048522,
7.2456237796818, 7.20965711830326, 7.1938623694745, 7.19583268801109,
7.19393445299217, 7.18816712299541, 7.20594221223447, 7.21663254411275
)), .Names = c("flag", "S", "T", "P", "pH", "CO2", "pCO2", "fCO2",
"HCO3", "CO3", "DIC", "ALK", "OmegaAragonite", "OmegaCalcite"
), row.names = 100:110, class = "data.frame")
答案 0 :(得分:0)
动物园对象是带有“coredata”的索引,它是一个矩阵。除非您发布dput()
的输出而不是控制台输出,否则我们没有任何对象。如果行号匹配,则应该起作用:
# coredata(testM) <- cbind( coredata(testZ) , data.matrix(testS) ) Wrong!
我不是动物园专家,因此发布dput
输出将有助于测试稍微不那么笨重的选项,并使用cbind(,,,)方法来理解你所看到的不满意的内容。 (我猜想它会起作用。)
require( chron )
cbind( testZ ,zoo(testS, order.by=as.chron( index(testZ) ) ) )
也许@ G.Grothendieck会来并教育我们......希望,希望。注意事项中添加了注释:
> str( cbind( testZ ,zoo(testS, order.by=as.chron( index(testZ) ) ) ) )
‘zoo’ series from (06/22/12 03:00:00) to (06/22/12 13:00:00)
Data: num [1:11, 1:19] 8.51 8.51 8.51 8.51 8.51 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:19] "X107b_pH" "X107b_SP" "X107b_temp" "Sal" ...
Index: Classes 'chron', 'dates', 'times' atomic [1:11] 15513 15513 15513 15513 15513 ...
..- attr(*, "format")= Named chr [1:2] "m/d/y" "h:m:s"
.. ..- attr(*, "names")= chr [1:2] "dates" "times"
..- attr(*, "origin")= num [1:3] 1 1 1970
> names(cbind( testZ ,zoo(testS, order.by=as.chron( index(testZ) ) ) ) )
[1] "X107b_pH" "X107b_SP" "X107b_temp" "Sal" "TApred" "flag"
[7] "S" "T" "P" "pH" "CO2" "pCO2"
[13] "fCO2" "HCO3" "CO3" "DIC" "ALK" "OmegaAragonite"
[19] "OmegaCalcite"
答案 1 :(得分:0)
我在尝试cbind数据框(x)和动物园对象(y)时遇到了同样的问题。 coredata(y)导致一个允许cbind工作的矩阵对象。因此
cbind(x, coredata(y))