0

I am trying to store the loop output. However, my dataset is quite big and it crashes Rstudio whenever I try to View it. I have tried different techniques such as the functions in library(iterators) and library(foreach), but it is not doing what I want it to do. I am trying to take a row from my main table (Table A)(number of rows 54000) and then a row from another smaller table (Table B)(number of rows = 6). I have also took a look at Storing loop output in a dataframe in R but it doesn't really allow me to view my results.

The code takes the first row from Table A and then iterates it 6 times through table B and then outputs the result of each iteration then moves to Table A's second row. As such my final dataset should contain 324000 (54000*6) observations.

Below is the code that provides me with the correct observations (but I am unable to view it to see it the values are being correctly calculated) and a snippet of Table A and Table B.

output_ratios <-  NULL

for (yr in seq(yrs)) {
  if (is.na(yr) == 'TRUE') {
    numerator=0
    numerator1=0
    numerator2=0
    denominator=0
  } else {
    numerator=Table.B[Table.B$PERIOD == paste("PY_", yr, sep=""), c("1")]
    denominator=Table.B[Table.B$PERIOD == paste("PY_", yr, sep=""), c("2")]
    denom=Table.A[, "1"] + (abs(Table.A[, "1"])*denominator)
    num=Table.A[, "2"] + (abs(Table.A[, "2"])*numerator)
    new.data$1=num
    new.data$2=denom
    NI=num / denom
    NI_ratios$NI=c(NI)
    output_ratios <<- (rbind(output_ratios, NI))
  }
}

TABLE B:

        PERIOD     1          2                3          4          5
1          PY_1  0.21935312 -0.32989691       0.12587413 -0.28323699 -0.04605116
2          PY_2  0.21328526  0.42051282      -0.10559006  0.41330645  0.26585064
3          PY_3 -0.01338112 -0.03971119      -0.06641667 -0.08238231 -0.05323772
4          PY_4  0.11625091  0.01127819       0.07114166  0.08501516  0.55676498
5          PY_5 -0.01269256 -0.02379182       0.39115278 -0.03716100  0.63530682
6          PY_6  0.69041864  0.51034273       0.59290357  0.78571429 -0.48683736 

TABLE A:

1      2      3     4
1     25     3657    2258
2     23     361361  250    
3     24     35      000 
4     25     362     502      
5     25     1039    502 

I would greatly appreciate any help.

3
  • when you say it iterates 6 times, you mean that it duplicates the row from table B six times ? Commented Jul 20, 2018 at 19:06
  • first, the code is long.. what are you trying to achieve?? eg numerator and denominator are exactly equal to each other. second there is no mention of new.data before its use. also there is nothing called NI_stress while you are outputting it.Lastly, whatever you are doing can be done by other functions other than iteration Commented Jul 20, 2018 at 19:09
  • Thank Onyambu, I have made the edits. Regarding new.data, I was trying to store the value for num and denum for debugging purpose. @yolo, It does not duplicate the row from table B. it takes a row from Table A and then multiplies it to Table B row by row (Table B contains 6 rows). Commented Jul 20, 2018 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.