我的数据如下:
SMA SMA.1 EMA SMA.2 SMA.1.1 EMA.1
2016-08-30 1.12411 1.114418 42.50321 100.9875 102.7288 61.25217
SMA,SMA.1和EMA都是针对一种变量进行的计算。我们称之为A1。同样,SMA.2 SMA.1.1,EMA属于A2。这一直持续到第29个变量A29。我想创建一个看起来像这样的表:
Var SMA SMA.1 EMA
A1 1.12411 1.114418 42.50321
A2 100.9875 102.7288 61.25217
我一直试图通过for()函数来解决这个问题,但它不起作用。如果它有用,请点击这里:
An ‘xts’ object on 2016-08-30/2016-08-30 containing:
Data: num [1, 1:87] 1.12 1.11 42.5 100.99 102.73 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:87] "SMA" "SMA.1" "EMA" "SMA.2" ...
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
NULL
> length(new.trend(MergedData))
[1] 87
> dput(new.trend(MergedData))
structure(c(1.12411, 1.114418, 42.5032053202923, 100.9875, 102.7288,
61.2521659094113, 1.30729, 1.312984, 48.6820468363914, 0.96929,
0.976116, 63.3550259067348, 0.762599999999999, 0.75914, 36.9475885995277,
1.29322, 1.301952, 58.0919871676451, 0.72508, 0.717614, 50.7849595338026,
8.431315, 8.518538, 60.1081873107905, 8.22937, 8.378464, 56.4004957184729,
24.03834, 24.251418, 57.4224323964315, 275.5605, 279.5882, 51.0296673272519,
3.7801, 3.814314, 49.384958216963, 3.82987000000001, 3.88755,
66.0031408704831, 64.45802, 64.756234, 52.7346708652452, 2.94456,
2.979396, 51.219478918934, 13.79045, 13.967152, 73.6027751334041,
3.2168, 3.231082, 54.8620717312901, 660.3725, 657.1164, 63.4249734975412,
2903.875, 2961.2792, 47.0703023915683, 18.34358, 18.526388, 59.4661365407602,
0.7604, 0.7558, 42.3745163443544, 6.65051000000001, 6.660192,
62.9781410895429, 13173.25, 13135.84, 73.4189545097276, 66.9848050000001,
66.9980439999999, 51.0467264878632, 1111.974, 1120.1316, 52.8805945280585,
4.01933, 4.022258, 59.7858956387081, 1.3497, 1.348972, 70.0217831036426,
46.48875, 46.79366, 45.2292753580903, 34.6466, 34.816, 37.0539681977138
), .Dim = c(1L, 87L), .Dimnames = list(NULL, c("SMA", "SMA.1",
"EMA", "SMA.2", "SMA.1.1", "EMA.1", "SMA.3", "SMA.1.2", "EMA.2",
"SMA.4", "SMA.1.3", "EMA.3", "SMA.5", "SMA.1.4", "EMA.4", "SMA.6",
"SMA.1.5", "EMA.5", "SMA.7", "SMA.1.6", "EMA.6", "SMA.8", "SMA.1.7",
"EMA.7", "SMA.9", "SMA.1.8", "EMA.8", "SMA.10", "SMA.1.9", "EMA.9",
"SMA.11", "SMA.1.10", "EMA.10", "SMA.12", "SMA.1.11", "EMA.11",
"SMA.13", "SMA.1.12", "EMA.12", "SMA.14", "SMA.1.13", "EMA.13",
"SMA.15", "SMA.1.14", "EMA.14", "SMA.16", "SMA.1.15", "EMA.15",
"SMA.17", "SMA.1.16", "EMA.16", "SMA.18", "SMA.1.17", "EMA.17",
"SMA.19", "SMA.1.18", "EMA.18", "SMA.20", "SMA.1.19", "EMA.19",
"SMA.21", "SMA.1.20", "EMA.20", "SMA.22", "SMA.1.21", "EMA.21",
"SMA.23", "SMA.1.22", "EMA.22", "SMA.24", "SMA.1.23", "EMA.23",
"SMA.25", "SMA.1.24", "EMA.24", "SMA.26", "SMA.1.25", "EMA.25",
"SMA.27", "SMA.1.26", "EMA.26", "SMA.28", "SMA.1.27", "EMA.27",
"SMA.29", "SMA.1.28", "EMA.28")), index = structure(1472533200, tzone = "", tclass = c("POSIXct",
"POSIXt")), .indexTZ = "", class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), tzone = "")
答案 0 :(得分:1)
简单:
new_data_mat <- matrix(data_mat[1,], ncol=3, byrow=T)
colnames(new_data_mat) <- c("SMA", "SMA2", "EMA")
答案 1 :(得分:1)
@ thc的答案肯定更简单,但如果你想留一个循环,这也有效
# take vector of your data
Data <- Data[1,]
names(Data)
New.Data <- NULL
EMAc <- 3; SMAc <- 1; SMA.1c <- 2
for (i in 1:(length(Data)/3)){
i.New.Data <- cbind(Var=paste("A",i,sep=""),
EMA=Data[i*EMAc],
SMA=Data[i*SMAc],
SMA.1=Data[i*SMA.1c])
New.Data <- rbind(New.Data,i.New.Data)
}
New.Data