-1

I am trying to code in r to find the corresponding runoff values , where , rainfall exceed certain threhold. Problem is that my for code , either take only one element , or does not store any iterations. I kept on getting error that argument length is >1 , only one lement is used It jsut keep on printing all values in the code , or just evalaute against last value. I want to test the condition row by row in the data frame

I have tried for loop , It has to be for loop as I cant just focus on built in function. Even If i change my values, it jsut keep on printing all values in the code

`

for(row in 1:nrow(post)){
 Rainfall <- post[row,"Runoff"]
 Date <- post[row, "Day"]
 if (Rainfall.m3[nrow(post)] > 1000){
 print(paste("on", Day,"runoff was",post$Runoff))
 }
 else
 {print("")
 }`}
}

Here is my Data

      Day OB.NO Rainfall.m3 Comulative.Rainfall   Runoff Comulative.Runoff Computed.Runoff Comulative.Computed
46  28-Dec    46      6177.3             88052.7 1567.000          8105.812       575.72436          8206.51164
47  13-Jan    47      3210.8             91263.5  376.650          8482.462       299.24656           8505.7582
48  19-Jan    48      1500.7             92764.2   96.156          8578.618       139.86524          8645.62344
49  20-Jan    49      1116.8             93881.0  293.412          8872.030       104.08576           8749.7092
50  23-Jan    50      1500.7             95381.7   78.744          8950.774       139.86524          8889.57444
51   5-Feb    51      3036.3             98418.0  192.096          9142.870       282.98316           9172.5576
52   6-Feb    52      1186.6             99604.6   31.164          9174.034       110.59112          9283.14872
53  12-Mar    53      1291.3            100895.9  128.172          9302.206       120.34916          9403.49788
54  20-Mar    54       802.7            101698.6  127.404          9429.610        74.81164          9478.30952
55  29-Mar    55       628.2            102326.8    8.226          9437.836        58.54824          9536.85776
56   9-Apr    56       732.9            103059.7   56.490          9494.326        68.30628          9605.16404
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
Shah
  • 31
  • 5
  • Data does not appear corectly but it contain 58 rows. and 10 columns , I dont know if there is better way to share the data – Shah Apr 24 '19 at 03:47
  • You could provide a reproducible example using something like `dput()`. See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Apr 24 '19 at 04:12
  • Odd, if your data is called `post`, you refer to `post[row,"Day"]` but then *just* `Rainfall.m3`, not `post[...,"Rainfall.m3"]`, is that intentional? And why is your comparison for `Rainfall.m3` always with the last of its values (`[nrow(post)]`), and not the row's specific value (`[row]`)? And then you refer to `Day` after having assigned to `Date`. **And** ... `post$Runoff` is a vector, not a singular value. – r2evans Apr 24 '19 at 06:49
  • Perhaps all you need (no `for` loop) is: `with(post[ post$Rainfall.m3 > 1000, ], sprintf("on %s runoff was %0.1f", Day, Runoff))`? – r2evans Apr 24 '19 at 06:53
  • r2evans, No , it’s not intentional , that’s what I am trying to understand , it could be my fault . I am trying to get and compare each value of row , to its threshold limit, but it only informs the last row. – Shah Apr 24 '19 at 08:12

1 Answers1

0

I confess I find it difficult to understand the context of this problem, but I have a hunch and maybe I’ll get lucky and hit on something.

Your loop iterates through a data frame, processing each row singly. I suspect this line has a problem.

print(paste("on", Day,"runoff was",post$Runoff))

I think “post$Runoff” references the entire Runoff column in your data frame. That could be why it keeps printing and printing. I think you need to select just one row.

Now you did that already a few lines back.

Rainfall <- post[row,"Runoff"]

Therefore, why not print that variable instead? You already have it.

print(paste("on", Day,"runoff was",Rainfall))

Hope that helps. I did my best.

Jai Jeffryes
  • 511
  • 5
  • 12
  • Thankyou for the response ! The thing is that , I am trying to loop over the entire data frame and figure our corresponding runoffs to , those rainfalls , where rainfall exceed certain threshold. I am continually getting the error , that it got more than one value and only first element is used. This lead to R holding only one value and then comapring only for that value while printing rest fo the data set , no deductions . – Shah Apr 24 '19 at 05:37
  • Further , when I follow your instruction , it only prints one value fir each number of row . That is the last value of data frames – Shah Apr 24 '19 at 08:46
  • Alright , without me doing anything , the code just worked , Just changed the computer , Is this the reason ? Here is the code which worked attach(post) Rainfall <- numeric(58) for(row in 1:nrow(post)){ Rainfall <- post[row,"Rainfall.m3"] Runoff <- post[row, "Runoff"] Date <- post[row, "Day"] if (Rainfall > 1500){ print(paste("on", Date,"Rainfall was",Rainfall, " Runoff was", Runoff)) } else { print(paste("The date:", Date, " is not important"))} } – Shah Apr 24 '19 at 10:00