0

我有两个数据框。我想将一个数据框中的一列添加到另一个数据框中。我不断收到以下错误消息 Coercing LHS to a list。

所以在下面,我想将数据框“y”中的“日期”列附加到数据框“x”。我试过使用 data.frame(cbind(x,y)) ,但它不返回日期值,而是一个我不知道它来自哪里的随机数。

   x= structure(c(0.00646807499999991, 0.0144789932290055,   0.0164372176821397, 
0.0192855441219475, 0.0137669110011083, 0.0131141698037536, 0.00480655053600088, 
-0.006705435719353, -0.0233800146043495, -0.0351887018698427, 
-0.0487182535979558, -0.0593401381065271, -0.0583906962441628, 
-0.048540233185126, -0.0369689068032834, 0.0125852550000001, 
 0.0189184796032502, 0.0268756083319235, 0.0254952903542085, 0.0255765095812046, 
0.0183501375047599, 0.0104742032843963, -0.00625200644290413, 
-0.0193244323675619, -0.0379181323327007, -0.0438453811416919, 
-0.0336959815318885, -0.0220850686056913, -0.0126664256753768, 
-0.00544005324554253, -0.00367231599999995, -0.00889830478082854, 
-0.0137005651484029, -0.0189736345840403, -0.0245762712517992, 
-0.0282956682507024, -0.0322975518895524, -0.0377589451648799, 
-0.0485404895704655, -0.0563088510546198, -0.0663370992355169, 
-0.0747645945341261, -0.0832391707255943, -0.0855461388462486, 
-0.0831450088769625, -0.00700431000000001, -0.00814176173800274, 
-0.00658524852328057, -0.00544779658015782, -0.00760296932482007, 
-0.0156848661567911, -0.0234674334920482, -0.0325071847183541, 
-0.0419659972285714, -0.0635775869322701, -0.082315613858488, 
-0.100035920406425, -0.0994372616007874, -0.102370690724894, 
-0.101352970283219, -0.00457328599999995, -0.0052536093922565, 
-0.00929775482225792, -0.0141356114680445, -0.024605034819042, 
-0.0370020414698513, -0.056995993832454, -0.091276740282481, 
-0.119245596518763, -0.151825534216445, -0.188033864671895, -0.217212185223669, 
-0.234825005773462, -0.249603144923882, -0.260337138206364,    0.0120747480000001, 
0.0130330613373864, -0.0243411604513616, -0.0677527561095079, 
-0.0256827993749988, -0.00335409830514233, -0.004791568662324, 
-0.0224245343100015, -0.0347867767079578, -0.0524197426256321, 
-0.0763775772566325, -0.0735984685055241, -0.0484906580285736, 
-0.0587446108219501, -0.0991854349530118, 0.0431361530000001, 
0.0787550936411081, 0.0573977104363048, 0.0236757769125835, 0.037094281760802, 
0.0746803436662813, 0.0898552771858721, 0.04503302044167, 0.030911972041697, 
-0.0337221437194116, -0.0603484605357663, -0.071027117300886, 
 -0.0262048603636683, -0.0136995915726184, -0.100744694776729, 
0.0374836169999999, 0.061992136058703, 0.0585845346356371, 0.0343381385843555, 
0.0277850582772103, 0.0824377445859632, 0.128440365877394,     0.0875491468161596, 
0.0288335505454778, -0.0519003947451165, -0.0615989531102824, 
-0.0576671052742732, -0.00642202002789749, 0.00537352413106573, 
-0.100000001679507, 0.037253397, 0.0897855952472022, 0.129871439255937, 
0.137940024073994, 0.149785691825778, 0.282832924696117, 0.435107543007464, 
0.325322202011322, 0.290128891187702, 0.175021661483114, 0.198455141978505, 
0.153476508089474, 0.258884336929128, 0.298712584717658, 0.164978742030078, 
0.00199481300000004, -0.0109714748257613, -0.0189507285012777, 
-0.0126670657322097, -0.00438859008824855, -0.00239377709368094, 
0.00349092244612326, 0.0173548760671445, 0.00907640001509269, 
0.0324157175745321, 0.0266307588386854, 0.0241372421195414,     0.023339316311461, 
 0.00718132678338357, 0.00139636731056192, 0.00409999999999999, 
 0.00811640000000002, 0.0121488655999999, 0.0163998908355201, 
 0.0196523704861937, 0.0229152580717495, 0.0263931699491937,    0.0291644315080564, 
 0.0313256768142234, 0.032666400194082, 0.0344219330744118, 0.0362838925539459, 
 0.0381492035605431, 0.0399140572065959, 0.0414739282924059), .Dim = c(15L, 
 11L), .Dimnames = list(NULL, c("boston", "dallas", "nyc", "chicago", 
 "la", "hyg", "ivv", "efa", "eem", "agg", "rf")))





y= structure(c(49L, 60L, 65L, 74L, 83L, 88L, 11L, 20L, 26L, 4L, 
39L, 43L, 50L, 58L, 67L), .Label = c("1/29/10", "1/30/09", "1/30/15", 
"1/31/08", "1/31/11", "1/31/12", "1/31/13", "1/31/14", "10/29/10", 
"10/30/09", "10/31/07", "10/31/08", "10/31/11", "10/31/12", "10/31/13", 
"10/31/14", "11/28/08", "11/28/14", "11/29/13", "11/30/07", "11/30/09", 
 "11/30/10", "11/30/11", "11/30/12", "12/30/11", "12/31/07", "12/31/08", 
 "12/31/09", "12/31/10", "12/31/12", "12/31/13", "12/31/14", "2/26/10", 
"2/27/09", "2/27/15", "2/28/11", "2/28/13", "2/28/14", "2/29/08", 
"2/29/12", "3/28/13", "3/30/12", "3/31/08", "3/31/09", "3/31/10", 
"3/31/11", "3/31/14", "4/29/11", "4/30/07", "4/30/08", "4/30/09", 
"4/30/10", "4/30/12", "4/30/13", "4/30/14", "5/28/10", "5/29/09", 
"5/30/08", "5/30/14", "5/31/07", "5/31/11", "5/31/12", "5/31/13", 
"6/28/13", "6/29/07", "6/29/12", "6/30/08", "6/30/09", "6/30/10", 
 "6/30/11", "6/30/14", "7/29/11", "7/30/10", "7/31/07", "7/31/08", 
"7/31/09", "7/31/12", "7/31/13", "7/31/14", "8/29/08", "8/29/14", 
"8/30/13", "8/31/07", "8/31/09", "8/31/10", "8/31/11", "8/31/12", 
"9/28/07", "9/28/12", "9/30/08", "9/30/09", "9/30/10", "9/30/11", 
"9/30/13", "9/30/14"), class = "factor")
4

1 回答 1

2

问题在于它x不是数据框,而是矩阵。cbind看到它是一个矩阵,并返回一个矩阵,y在这个过程中强制转换为一个数字向量,因为矩阵不能有多个数据类型。

您可以cbind.data.frame按照评论使用。或者这样做可能更清楚:

x <- as.data.frame(x)
cbind(x, y)

或者更好,

x <- as.data.frame(x)
x$y <- y

因为它强制您为y. 如果长度错误,这也会引发错误y,而其他只会引发警告。

于 2015-05-04T05:18:40.020 回答