0

I am fetching bins.txt and saving its data in "data". I tried printing it and it is printing properly.

data <- read.csv("bins.txt", header = FALSE)
for (n in 1:24060)
   {
   j=(data[n,])
    for (i in 1:20)
    {
      m=(i-1)*80
      n=(i*80)-1
      if(m<j && j<n)
      {        
        print (i)
      } 


    }
  }

I wish to not print(i) but store the values of i in some vector and print it outside the loop and pass it in

obs="vector" 

Somewhat like this

Arushi
  • 69
  • 9
  • Can you provide a sample data which represents your problem and the equivalent expected output? – Ronak Shah May 23 '16 at 03:55
  • You'd need to define a vector before the loop and assign to it, but you almost certainly don't need loops. A hint: `if` is not vectorized, but `ifelse` is. If you [explain your problem well](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), you'll probably get a full answer. – alistaire May 23 '16 at 03:58

1 Answers1

0

No idea what your bins.txt is. Since I really dislike nested loops, here's a suggestion:

(i) define the twenty pairs of min (or m) and max (or j) values in condition check:

 m <- lapply(1:20, function(x) (x-1)*80)
 n <- lapply(1:20, function(x) (x*80)-1)

(ii) return a list of twenty vectors based against data based on the twenty combinations of m and n:

lapply(1:20, function(x) dat[m[[x]] < dat &  dat < n[[x]]])

Assuming that your data is

dat <- seq(0, 1000, length.out=50)

The first six vectors returned are:

[[1]]
[1] 20.40816 40.81633 61.22449

[[2]]
[1]  81.63265 102.04082 122.44898 142.85714

[[3]]
[1] 163.2653 183.6735 204.0816 224.4898

[[4]]
[1] 244.8980 265.3061 285.7143 306.1224

[[5]]
[1] 326.5306 346.9388 367.3469 387.7551

[[6]]
[1] 408.1633 428.5714 448.9796 469.3878
Adam Quek
  • 6,973
  • 1
  • 17
  • 23