我对R来说真的很陌生,我仍然试图绕过()函数。我的数据dput(MergedData)链接here(我为篇幅道歉;我试图尽可能地缩短它)。我正在做一个小项目,我有以下功能:
new.trend <- function(MergedData)
{
ret <- as.list(rep(NA, length(MergedData)))
ma.sig <- ma.crossover(MergedData)
pricebreak <- price.channel(MergedData)
sig <- intersect(which((ma.sig[1,])==1), which(!pricebreak[1,]==0))
for (i in sig) { #Calculates output variables based on active signals
x <- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
dev20 <- (x[,4]-SMA(x[,4], n=20))/x[,4]*100
dev50 <- (x[,4]-SMA(x[,4], n=50))/x[,4]*100
RSI <- RSI(x[,4], n=14)
ret[[i]]<- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L)))
}
na.omit(print(ret))
}
print(new.trend(MergedData))
问题/问题
return(ret)返回此:
> new.trend(MergedData)
[[1]]
EUR.LAST EUR.LAST.1 EMA
2017-02-09 -0.6968559 0.3526983 44.68176
[[2]]
[1] NA
[[3]]
GBP.LAST GBP.LAST.1 EMA
2017-02-09 -0.1920461 1.027927 52.27664
[[4]]
CHF.OPEN CHF.OPEN.1 EMA
2017-02-09 0.5066387 -0.7241689 52.56533
[[5]]
[1] NA
[[6]]
[1] NA
[[7]]
[1] NA
[[8]]
[1] NA
[[9]]
[1] NA
[[10]]
[1] NA
[[11]]
[1] NA
[[12]]
[1] NA
[[13]]
PLN.CLOSE PLN.CLOSE.1 EMA
2017-02-09 0.2824105 -1.569392 48.24069
[[14]]
[1] NA
[[15]]
TRY.CLOSE TRY.CLOSE.1 EMA
2017-02-09 -2.315328 -0.2501765 42.52731
[[16]]
ZAR.CLOSE ZAR.CLOSE.1 EMA
2017-02-09 -0.09598239 -1.492148 46.06286
[[17]]
[1] NA
[[18]]
CLP.CLOSE CLP.CLOSE.1 EMA
2017-02-09 -0.2433194 -2.112368 40.93616
[[19]]
[1] NA
[[20]]
MXN.CLOSE MXN.CLOSE.1 EMA
2017-02-09 -2.460443 -3.490762 34.67792
[[21]]
PEN.CLOSE PEN.CLOSE.1 EMA
2017-02-09 -0.4138617 -1.974541 37.84737
[[22]]
CNY.CLOSE CNY.CLOSE.1 EMA
2017-02-09 -0.08749199 -0.5004658 44.39283
[[23]]
IDR.CLOSE IDR.CLOSE.1 EMA
2017-02-09 -0.4064827 -0.631571 35.91677
[[24]]
INR.CLOSE INR.CLOSE.1 EMA
2017-02-09 -1.291429 -1.594705 21.83156
[[25]]
KRW.CLOSE KRW.CLOSE.1 EMA
2017-02-09 -0.8529425 -2.840274 34.61214
[[26]]
MYR.CLOSE MYR.CLOSE.1 EMA
2017-02-09 0.1407816 -0.4020273 49.80231
[[27]]
SGD.CLOSE SGD.CLOSE.1 EMA
2017-02-09 0.123548 -0.7103133 49.73621
[[28]]
PHP.CLOSE PHP.CLOSE.1 EMA
2017-02-09 0.1355443 0.236601 55.61772
[[29]]
THB.CLOSE THB.CLOSE.1 EMA
2017-02-09 -0.518655 -1.396926 23.51997
但我想检索for()函数中打印的内容,例如:
> for (i in sig) { #Calculates output variables based on active signals
+ x <- MergedData[[i]]
+ x <- xts(x[,-1], order.by=x[,1])
+ dev20 <- (x[,4]-SMA(x[,4], n=20))/x[,4]*100
+ dev50 <- (x[,4]-SMA(x[,4], n=50))/x[,4]*100
+ RSI <- RSI(x[,4], n=14)
+ print(ret[[i]]<- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L))))
+ }- (x[,4]-SMA(x[,4], n=50))/x[,4]*100
RSI <- RSI(x[,4], n=14)
print(ret[[i]]<- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L))))
}
EUR.LAST EUR.LAST.1 EMA
2017-02-09 -0.6968559 0.3526983 44.68176
GBP.LAST GBP.LAST.1 EMA
2017-02-09 -0.1920461 1.027927 52.27664
CHF.OPEN CHF.OPEN.1 EMA
2017-02-09 0.5066387 -0.7241689 52.56533
PLN.CLOSE PLN.CLOSE.1 EMA
2017-02-09 0.2824105 -1.569392 48.24069
TRY.CLOSE TRY.CLOSE.1 EMA
2017-02-09 -2.315328 -0.2501765 42.52731
ZAR.CLOSE ZAR.CLOSE.1 EMA
2017-02-09 -0.09598239 -1.492148 46.06286
CLP.CLOSE CLP.CLOSE.1 EMA
2017-02-09 -0.2433194 -2.112368 40.93616
MXN.CLOSE MXN.CLOSE.1 EMA
2017-02-09 -2.460443 -3.490762 34.67792
PEN.CLOSE PEN.CLOSE.1 EMA
2017-02-09 -0.4138617 -1.974541 37.84737
CNY.CLOSE CNY.CLOSE.1 EMA
2017-02-09 -0.08749199 -0.5004658 44.39283
IDR.CLOSE IDR.CLOSE.1 EMA
2017-02-09 -0.4064827 -0.631571 35.91677
INR.CLOSE INR.CLOSE.1 EMA
2017-02-09 -1.291429 -1.594705 21.83156
KRW.CLOSE KRW.CLOSE.1 EMA
2017-02-09 -0.8529425 -2.840274 34.61214
MYR.CLOSE MYR.CLOSE.1 EMA
2017-02-09 0.1407816 -0.4020273 49.80231
SGD.CLOSE SGD.CLOSE.1 EMA
2017-02-09 0.123548 -0.7103133 49.73621
PHP.CLOSE PHP.CLOSE.1 EMA
2017-02-09 0.1355443 0.236601 55.61772
THB.CLOSE THB.CLOSE.1 EMA
2017-02-09 -0.518655 -1.396926 23.51997
提示,建议和指示将非常感谢!我知道for()函数可能并不理想,但它是我知道如何仅循环通过与sig中索引相对应的MergedData值的唯一方法。
答案 0 :(得分:0)
好的,现在我明白了。如果你想保持循环,你可以改变:
na.omit(print(ret))
到
lapply(ret, function(x) x[!is.na(x)])
答案 1 :(得分:0)
尝试使用apply函数来评估数据帧或动物园时间序列。
在R中,最好避免使用lapply
之类的结构。使用产生列表的apply
或给出矩阵的library(dplyr)
library(zoo)
#create zoo object
z <- zoo(data.frame(column1 = c(1, 2, 3), column2 = c(5, 6, 7)), order.by = seq(from = as.Date('2017-01-01'), by = 'day', length.out = 3))
z
#create function to calculate each row
f <- function(row){
row ^ 2
}
#apply function f to each row (MARGIN = 1) of zoo (z) object, transpose matrix and create zoo time series
res<-apply(z,MARGIN = 1,function (row) f(row)) %>% t %>% as.zoo(.,order.by=rownames(.))
res
class(res)
。
考虑这个例子:
> library(dplyr)
> library(zoo)
> #create zoo object
> z <- zoo(data.frame(column1 = c(1, 2, 3), column2 = c(5, 6, 7)), order.by = seq(from = as.Date('2017-01-01'), by = 'day', length.out = 3))
> z
column1 column2
2017-01-01 1 5
2017-01-02 2 6
2017-01-03 3 7
> #create function to calculate each row
> f <- function(row){
+ row ^ 2
+ }
> #apply function f to each row (MARGIN = 1) of zoo (z) object, transpose matrix and create zoo time series
> res<-apply(z,MARGIN = 1,function (row) f(row)) %>% t %>% as.zoo(.,order.by=rownames(.))
> res
column1 column2
2017-01-01 1 25
2017-01-02 4 36
2017-01-03 9 49
> class(res)
[1] "zoo"
>
这将产生
printenv