【问题标题】:How to find the Average by rows using R如何使用 R 按行查找平均值
【发布时间】:2014-09-13 22:09:06
【问题描述】:

我正在尝试在 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 = "")

【问题讨论】:

  • 你试过?rowMeans吗?

标签: r xts mean


【解决方案1】:

怎么样

cbind(z,rowMeans(z))

要捕获列名,请执行以下操作:

cbind(z, AVGRET = rowMeans(z))

【讨论】:

  • 实际上,在问题的上下文中,因为z 是一个矩阵,所以这是正确的答案。也许更改为cbind(z, AVGRET = rowMeans(z)) 以获取列名
  • @RichardScriven 这就是我想要的。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 2017-10-25
相关资源
最近更新 更多