Reputation: 889
I have a data.frame (df) which contains two columns (time, Result). I'm trying to loop inside the data frame based on the time and check the value of the Result to perform some condition. In other words, I want to go over the df from the beginning until the df$time value becomes 60. For each iteration I need to check the df$Result if it is greater than 100. I have done this by using subset, however, I'm wondering how to do it in different way..for example nested For loop.
I have created A MWE to illustrate the question;
time<-seq(1,100,0.1)
Result<-seq(1,991,1)
df<-data.frame(time,Result)
# I want to loop inside the df until the df$time=60
# for each iteration I want to check the df$Result if it's >100.
Here is my try to do it in nested :
Df_time<-df$time
Df_result<-df$Result
x<-0
for(i in Df_time){
if(i > 60.0){
for(i in Df_result){
if(i >100){
x<-x+1
}
}
}
}
cat("Total is ",x,"\n")
It doesn't seem right .. which I think related to the inner for loop as it will go over the whole range... Any suggestions?
Upvotes: 1
Views: 732
Reputation: 12905
This should give you the count of the number of rows, I think that' what you're trying to add up as x
dim(df[time < 60 & Result > 100,])[1]
Upvotes: 2