2017-04-30 1 views
0

도움이 필요하십니까? 코드의이 부분에서루핑 오류 - 개체를 찾을 수 없음

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에 의해 행) dataframe/7 가지 다른 독특한 손상의 행렬이다 유형 (damageType 열, 아래 코드에서 찾을 수 있음). 임의로 선택한 0/1 값 10,000 개가 있습니다.

코드 다시 :

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

한 가지 가능한 방법을 얻을에 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 흠을 준다. 나는 당신의 모든 과정을 확신하지 못하기 때문에 조금 혼란 스럽다. freqSim <- infreqDF ('Software')에서와 같이 함수 호출을 할당하면 어떨까요? –

관련 문제