2014-09-12 66 views
2

我试图找到行的平均R.如何找到利用R

# Read into R 
read.csv("BOOK1.csv", header=TRUE)-> STOCKS 

# Convert to xts 
z = xts(STOCKS[,2:5], order.by=as.POSIXct(STOCKS[,1], format="%Y-%m-%d")) 

的XTS对象zREPRODUCIBLE DATA下排的平均值。我想在向量z的末尾添加一列,并为该日给定的所有股票的平均值,以便按行返回平均值。这是我想达到的目标:

# Desired Output 
desired <- structure(c(-0.020576, 0.008403, 0.033333, 0, -0.016129, 0, 0.012295, 
0.004049, 0.004032, -0.008032, -0.032389, -0.012552, -0.033898, 
0.030702, 0, -0.012766, -0.012931, 0.030568, -0.012712, 0.027027, 
0, 0.026316, 0.051282, -0.025, 0.025641, 0, 0, -0.0125, -0.012658, 
0, 0, -0.025641, 0.052632, -0.0125, 0.012658, 0, -0.05, 0, 0.083333, 
0.009615, 0.009524, 0, 0, 0, -0.018868, 0.038462, 0, 0.037037, 
-0.017857, -0.018182, -0.037037, -0.038462, 0, 0.04, -0.019231, 
0.019608, 0, -0.016447, -0.003344, 0, 0.003356, 0.010033, -0.006623, 
0.003333, 0.003322, 0.003311, 0.0033, 0, -0.006579, -0.006623, 
-0.013333, 0.006757, -0.006711, 0, 0.013514, 0, 0.01833425, 0.0036685, 
0.01729325, 0.0136595, -0.007774, 0.0047545, -0.00081, 0.01145825, 
-0.00128925, 0.00491175, -0.0125615, -0.00932825, -0.02579975, 
0.00788475, -0.00143575, 0.00829525, -0.0080405, 0.0034225, -0.003178 
), .Dim = c(19L, 5L), .Dimnames = list(NULL, c("STOCK.A", "STOCK.B", 
"STOCK.C", "STOCK.D", "AVGRET")), index = structure(c(-915033600, 
-914947200, -914860800, -914688000, -914601600, -914515200, -914428800, 
-914342400, -914256000, -914083200, -913996800, -913910400, -913824000, 
-913737600, -913651200, -913478400, -913392000, -913305600, -913219200 
), tzone = "", tclass = c("POSIXct", "POSIXt")), class = c("xts", 
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "") 

重复性的数据

z <- structure(c(-0.020576, 0.008403, 0.033333, 0, -0.016129, 0, 0.012295, 
0.004049, 0.004032, -0.008032, -0.032389, -0.012552, -0.033898, 
0.030702, 0, -0.012766, -0.012931, 0.030568, -0.012712, 0.027027, 
0, 0.026316, 0.051282, -0.025, 0.025641, 0, 0, -0.0125, -0.012658, 
0, 0, -0.025641, 0.052632, -0.0125, 0.012658, 0, -0.05, 0, 0.083333, 
0.009615, 0.009524, 0, 0, 0, -0.018868, 0.038462, 0, 0.037037, 
-0.017857, -0.018182, -0.037037, -0.038462, 0, 0.04, -0.019231, 
0.019608, 0, -0.016447, -0.003344, 0, 0.003356, 0.010033, -0.006623, 
0.003333, 0.003322, 0.003311, 0.0033, 0, -0.006579, -0.006623, 
-0.013333, 0.006757, -0.006711, 0, 0.013514, 0), .Dim = c(19L, 
4L), .Dimnames = list(NULL, c("STOCK.A", "STOCK.B", "STOCK.C", 
"STOCK.D")), index = structure(c(-915033600, -914947200, -914860800, 
-914688000, -914601600, -914515200, -914428800, -914342400, -914256000, 
-914083200, -913996800, -913910400, -913824000, -913737600, -913651200, 
-913478400, -913392000, -913305600, -913219200), tzone = "", tclass = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "") 
+1

你有没有尝试'?rowMeans'? – 2014-09-12 23:08:56

回答

2

如何

cbind(z,rowMeans(z)) 

要捕捉的列名,这样做:

cbind(z, AVGRET = rowMeans(z)) 
+1

事实上,在问题的背景下,因为'z'是一个矩阵,所以这是正确的答案。也许改为'cbind(z,AVGRET = rowMeans(z))'来获得列名 – 2014-09-12 23:15:12

+0

@RichardScriven这就是我正在寻找的。谢谢 – Rime 2014-09-12 23:19:57

+0

@RichardScriven补充说,谢谢 – 2014-09-13 14:13:23