For loop & if else working for less data but not working for more data

心不动则不痛 提交于 2019-12-24 07:42:21

问题


Calculation inside for loop & ifelse is working when I have 100-200 rows but not working when I have 20000 rows.

Can someone help me with the FOR loop and IFELSE if something is wrong or if there is some timeout happening in R studio when running for & if-else loop

Code:

#FROM HERE IT IS NOT WORKING WHEN WE HAVE 20000 ROWS OF DATA IN FINAL DATFRAME. 
#WE ARE CREATING FINAL_V1 WHICH IS POPULATING ONLY 1 ROW




#New Dataframe with Null values



Final <- structure(list(Item = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "0S1576", class = "factor"), 
               LC = structure(1:6, .Label = c("MW92", "OY01", "RM11", "RS11", 
                                              "WK14", "WK15"), class = "factor"), Fiscal.Week = structure(c(1L, 
                                                                                                            1L, 1L, 1L, 1L, 1L), .Label = "2019-W24", class = "factor"), 
               SS = c(15L, 7L, 5L, 9L, 2L, 2L), Freq = c(3, 6, 1, 2, 1, 
                                                         1), agg = c(1, 1, 1, 1, 0, 0)), row.names = c(NA, -6L), class = "data.frame")

lctolc <- structure(list(Item = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "0S1576", class = "factor"), 
                         LC = structure(c(1L, 2L, 2L, 3L, 3L), .Label = c("MW92", 
                                                                          "OY01", "RM11"), class = "factor"), ToLC = structure(1:5, .Label = c("OY01", 
                                                                                                                                               "RM11", "RS11", "WK14", "WK15"), class = "factor")), row.names = c(NA, 
                                                                                                                                                                                                                  -5L), class = "data.frame")


df <- as.data.frame(unique(Final$Item))
Final_v1 <- NA
j <- 1
i <- 1


#SS computations
#For 1 to no of rows in df(which is having no of unique items 
for(j in 1:nrow(df)) {
  #copying the data from Final to Final_v1(with charater type)
  Final_v1 <- Final[Final$Item == as.character(df[j,1]),]
  #for 1 to the no of rows in Final_v1
  for(i in 1:nrow(Final_v1)) {

    if(Final_v1[i,6] <= 0)


      { 
        Final_v1[i,7] = Final_v1[i,4]}

      else
      { 
    if(Final_v1[i,5] == '1')
    {
      Final_v1[i,7]=0
      }

    else 
    {
      Final_v1[i,7]=Final_v1[i,4]
      }
    SSNew <- Final_v1[i,7]

    #Leftover distribution
    LCS <- lctolc$ToLC[Final_v1$Item[i] == lctolc$Item & Final_v1$LC[i] == lctolc$LC]
    inds <- Final_v1$LC %in% LCS    
    if (any(inds))
    {  Final_v1$SS[inds]<- if (SSNew == 0) {Final_v1$SS[inds]==0} else {Final_v1$SS[inds]=Final_v1$SS[inds]}    }
    }



  }

  names(Final_v1)[7] <- "SSNew"
}

Can someone help why it is not performing for 20000rows

来源:https://stackoverflow.com/questions/57182385/for-loop-if-else-working-for-less-data-but-not-working-for-more-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!