need some help with an assignment

Hi I'm doing an assignment where we take a cvs and try to explain what's happening.
I'm stuck with this part right here and I was wondering if anyone could explain to me step by step what is going on here: :

y=0
> n=0
> for(i in seq(1,nrow(matches_data)))
  + {
    +     if (matches_data$toss_winner[i] == matches_data$winner[i])
      +         y=y+1
      +     else 
        +         n=n+1
        + }
> 
  > if (y >= n)
    + {
      +     print(paste("Yes, Toss-winning helped in winning matches."))
      +     print(paste("Matches won by toss_winner are: ", y, "& Total matches: ", nrow(matches_data)))
      + } else
        +     
  + {
    +     print(paste("No, Toss-winning didn't help in winning matches."))
    +     print(paste("Matches won by other team are: ", n, "& Total matches: ", nrow(matches_data))) 
    + }
[1] "Yes, Toss-winning helped in winning matches."
[1] "Matches won by toss_winner are:  325 & Total matches:  636"
> ## [1] "Yes, Toss-winning helped in winning matches."
  > ## [1] "Matches won by toss_winner are:  325 & Total matches:  636"

I think this technically comes under the FAQ: Homework Policy so I am not sure how much direct explanation is acceptable but at a start have a look at For loops and, perhaps
read the help entries and their examples for

?seq
?nrow

If this does not help please come back and someone here should be able to answer specific questions.

Just to be on the save side, can you supply a FAQ: How to do a minimal reproducible example ( reprex ) for beginners ?

1 Like

I have completed 80% of the assignment and this is the last part.
Ive tried to google the different functions to get an understanding but can't seem to find a good answer, that's why I made a post here, any help would be grateful :slight_smile:

Maybe you could say a bit more about what you don't understand? Try and be as specific as possible. What exactly is it that you aren't following?

im a beginner and very new to this, so mostly every code.
like
y=0, n=0
for(i in seq(1,nrow(matches_data)))

  • {
    • if (matches_data$toss_winner[i] == matches_data$winner[i])
      
      •     y=y+1
        
      • else 
        
        •     n=n+1
          

what does this function do?

What you are looking at is a common for-loop. Its structure is for (enumerator in range) {do stuff}. Let´s have a closer look at it.

1.) y = 0; n = 0
This part initializes two numeric objects in your global environment (this is where everything you can directly adress lives, like values, functions, data.frames, you get the drill). They will be used in the for-loop and need to be initialized, since otherwise the loop won't know what to do if it encounters a part which references those objects.

2.) for (i in seq(1 ,nrow(matches_data))){

This starts the loop. The enumerator is i and it runs from 1 to the number of rows matches_data has. This is done with the seq() (short for sequence) function. The "checking" is done stepwise for every i in the sequence given from 1 to the number of rows of the match_data data set.

3.) if (matches_data$toss_winner[i] == matches_data$winner[i]) { y = y + 1}

The if clause checks, if a condition is met (here: toss winner and match winner are equal?). If it is the case, add 1 to y (which has been initialized above).

4.) } else { n = n + 1}

The else clause ticks n up by 1 if the if clause fails.

The second part of the for loop from the initial question follows the same logic. If y >= n is true, print something, otherwise print something else.

Kind regards

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.