0

I explain with an example, I insert a number (for example, my input is 2010000) and I need the ccompare sum of the values in the third row with my input (my condition : the summation of the elements in the third row <= my input), Until my condition is True, I need to get the values in the first and the second rows. for example in the case of 2010000, I need to get

1   2  3  4
-1 -1 -1 -1

P.S : I should not read the last positive value of the third row.

I could do it with for loops but as you know loops are not fast for R.

Thanks

My Data:

 1       2       3       4      10      37       NA
-1      -1      -1      -1      -1      -1      -6
549493  217514  732961  506807  113712  139339  2259826
Ester Silva
  • 670
  • 6
  • 24
  • Your Data is not [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Sotos Sep 29 '16 at 14:47
  • I think it would be easier if you transpose your dataset (`t_df <- as.data.frame(t(df))`) and do `t_df[which(cumsum(t_df$V3) <= 2010000), 1:2]` – Sotos Sep 29 '16 at 14:52
  • Thanks @Sotos. But I guess your answer should be `t_df[1:2,which(cumsum(t_df$V3) <= 2010000)]`. :) – Ester Silva Sep 29 '16 at 15:02
  • Nope. I transposed the data frame so it is correct – Sotos Sep 29 '16 at 15:28

1 Answers1

0

Your solution could possibly be

#setting up a data frame with your data structure 
row1 <- c(1,2,3,4,10,37,NA)
row2 <- c(-1,-1,-1,-1,-1,-1,-6)
row3 <- c(549493,217514,732961,506807,113712,139339,2259826)

df <- rbind(row1,row2,row3)

#generating a row4 with sums upto that point 
row4 <- sapply(1:length(df[3,]),function(x) sum(df[3,1:x])) 

#your input 
input <- c(2010000)

#logical vector where your condition is reached 
row5 <- row4<input

#index where the summation is <= input
index <- min(which(row5 == FALSE))

#your results 
answer_row1 <- row1[1:index]
answer_row2 <- row2[1:index]

#youv can format your results which ever way you want now that you 
#have the value of #index

Let me know if this works

Lune3141

Lune3414
  • 144
  • 1
  • 9