2017-04-30 8 views
0

ヘルプが必要です!コードのこの部分からルーピングエラー - オブジェクトが見つかりませんR

Error: object 'freqSim' not found 

:このエラーを取得し続ける理想的

indfreqDF <- function(x){ 
    frame <- severity[severity$industryType == x,] 
    weightPercent <- frame$relfreq 

    z <- largeAvgFunc2(freqDF)*weightPercent 
    z <- z[-c(8)] 
    # run a for loop to simulate 0/1 dataframe for each loss type 

    for (i in 1:7){ 
    freqSim[i] <<- data.frame(sample(0:1, length(1:10000), replace=T, prob = c(1-z[i],z[i]))) 
    } 
    return(freqSim[i]) 
} 

indfreqDF("Software") 
    Error in freqSim[i] <<- data.frame(sample(0:1, length(1:10000), replace = T, : 
    object 'freqSim' not found 

、私が希望する10,000x7は(COLによって行)データフレーム/ 7異なるユニーク損傷の行列であります10,000個のランダムに選択された0/1値を持つ型(damageType列、以下のコード内にあります)。

コードは再作成する方法:

dec_amt <- function(x, k) format(round(x, k), nsmall=k) 
severity <- data.frame(industryType = c("Consumer Products", 
            "Biotech/Pharma", 
            "Industrial/Construction", 
            "Computer Hardware/Electronics", 
            "Medical Devices", 
            "Software", 
            "Business/Consumer Services", 
            "Telecommunications", 
            "Automotive/Transportation", 
             "Chemicals/Synthetic Materials", 
             "All Industries"), 
            relfreq = c(2.032520, 
               0.650407, 
               1.327913, 
               1.571816, 
               1.463415, 
               0.758808, 
               0.623306, 
               0.650407, 
               0.460705, 
               0.460705, 
               1.000000), 
             relsev = c(0.419048, 
               3.771429, 
               0.609524, 
               2.019048, 
               3.028571, 
               1.314286, 
               0.723810, 
               4.247619, 
               0.152381, 
               0.076190, 
               1.000000))  

largeAvgFunc2 <- function(x) { 
      three <- apply(x, 2, function(y) mean(tail(y,10))) 
      four <- apply(x, 2, function(y) mean(tail(y,5))) 

      avgMu2 <- apply(cbind(three,four), 1, mean) 

      return(avgMu2) 
      } 


PIRATE <- data.frame(damageType = c("RR","RR","RR","LP","LP","LP","CD","CD","CD","ED","ED","ED","AF","AF","AF","CS","CS","CS","PI","PI","PI","TD","TD","TD") 
       ,Year = c(2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002, 
        2000,2001,2002,2000,2001,2002), 
       Count = c(4,4,9,18,17,20,15,15,12,12,12,12,15,12,12,53,1,3,5,6,7,2,12,6), 
       Amount = c(35222,12512012,12512,1251,1251251,12151,7771,1091,1091,121,121,194,1512,125125,125,621, 
          2631,136161,1321236,2136,111,3213,1262,6610)) 
PIRATE <- transform(PIRATE, medAmount = Amount/Count) 
PIRATE <- transform(PIRATE, freqYearly = Count/c(18, 
              16, 
              45)) 

PIRATE$freqYearly <- dec_amt(PIRATE$freqYearly, 4);PIRATE$freqYearly <- as.numeric(PIRATE$freqYearly) 
freqDF <- data.frame(PIRATE$damageType, PIRATE$freqYearly, PIRATE$Year) 
freqDF <- acast(freqDF, PIRATE.Year ~ PIRATE.damageType, value.var = 'PIRATE.freqYearly') 


indfreqDF <- function(x){ 
     frame <- severity[severity$industryType == x,] 
     weightPercent <- frame$relfreq 

     z <- largeAvgFunc2(freqDF)*weightPercent 
     z <- z[-c(8)] 

     # run a for loop to simulate 0/1 dataframe for each loss type 

      for (i in 1:7){ 
      freqSim[i] <<- data.frame(sample(0:1, length(1:10000), replace=T, prob = c(1-z[i],z[i]))) 
     } 
     return(freqSim[i]) 
     }           

indfreqDF("Software") 
+0

てみ 'freqSIM < - あなたのループの前にリストを()'。私はあなたが '' - ローカルオブジェクトに割り当てるために必要なとは思っていません - 単に '< - 'を使ってください。あなたは最後のサンプルを返すだけです。 – epi99

+0

事は後の関数にありますが、後で使用するためにグローバル変数として保存する必要があります。 – S31

答えて

1

1つの可能な方法を取得するためにindfreqDFを変更することがあり、次のようになります。

indfreqDF <- function(x){ 
    frame <- severity[severity$industryType == "Software",] 
    weightPercent <- frame$relfreq 

    z <- largeAvgFunc2(freqDF)*weightPercent 
    z <- z[-c(8)] 

    # run a for loop to simulate 0/1 dataframe for each loss type 

    freqSim <- list() 
    for (i in 1:7){ 
    freqSim[[i]] <- data.frame(sample(0:1, length(1:10000), replace=T, prob = c(1-z[i],z[i]))) 
    } 

    do.call(cbind, freqSim) 

} 
+0

だから閉じる!それは動作します、私はまだfreqSimをグローバル変数にする必要があるので、後で呼び出すことができます。ですから、freqSim [[i]] << - などを割り当てても、私には同じエラーが返されます。 – S31

+0

@ S31 Hmm。あなたの全体のプロセスがわからないので、少し混乱しています。 freqSim < - infreqDF( 'Software')のように関数呼び出しを割り当てるとどうなりますか? –

関連する問題