2017-02-12 41 views
0

我對R真的很陌生,我仍然試圖圍繞着()函數進行打包。我對我的數據(MergedData)的示例輸入鏈接here(我對此長度表示歉意;我試圖儘可能縮短它)。我工作的一個小項目,我有以下功能:將for()函數中的打印值保存爲變量?

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)) 

問題/問題

回報率(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

好的,現在我明白了。如果你想保持對循環你可以更改:

na.omit(print(ret)) 

lapply(ret, function(x) x[!is.na(x)]) 
+0

感謝您的建議,但出於某種原因,我仍然得到同樣的結果na.omit(打印(RET) ) – Nikitau

+0

我真的很感謝第二次嘗試:),但它給了我「在[.xts'(x,!is.na(x))中出錯:'i'或'j'超出範圍。我也試過na.omit(do.call(ret,merge)),但它返回了一個空矩陣。 – Nikitau

+0

嗯...我試圖用dget加載數據,但R失敗。你可以轉儲dput的數據intead嗎? – kwicher

0

嘗試使用應用函數來評估數據幀或動物園的時間序列。 在R中,最好避免構造如ret <- as.list(rep(NA, length(MergedData))); ret[[i]]<-row。使用產生列表的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" 
> 
+0

謝謝!我試圖成爲一個更好的R程序員,所以風格指針非常感謝:)。是否有理由申請更優於ret < - as.list(rep(NA,length(MergedData))); RET [[I]] < - R'我對應用功能並不熟悉,但我會盡力瞭解它! for()函數是否適用於/ lapply?我真的只想讓函數運行某些MergedColumn的索引 – KeynesCrackpot

+0

'apply'允許你管理數組索引,它使你的代碼變得清晰和簡短。你將避免許多從普通數組下降的mistskes。 'for'循環可能適用於內部,但在R中矢量化解決方案更加優雅,這是爲了節省您的時間。嘗試在R中使用不是循環的向量。 – Alexander

+1

請注意,除了'%>%',這不會使用dplyr以外的任何內容,並且它是從magrittr導入的,所以'library(dplyr)'可以替換爲'library(magrittr)'來最小化依賴關係。 –

相關問題