0

I need to calculate the number of 4's and 6's a batsman has scored in a given cricket database.

The following is my code:

runs_batsman = ballbyball[,c("Batter","RunsBatsman")]

fours = data.frame(bat_name)
numoffours = function(name)
{counter = 0
for(i in nrow(ballbyball))
 {
if((identical(toString(name),toString(runs_batsman[i,1]))))
  {
  if(runs_batsman[i,2]== 4)
    {
    counter = (counter + 1)
    }

   }
}
 return(counter)
}

summary = function(dataset){
fours[,"numfours"]=0
for (j in nrow(bat_name)){
  fours[j,2] = lapply(bat_name[j,1], numoffours)
 }
 return(fours)
}

I am not getting any values for any of the batsmen. bat_name is the data frame with all batsmen names. Please help me.

I am very new to R so please explain in much detail as possible.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • 2
    Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 May 16 '16 at 06:49
  • It is hard to help you without knowing anything about your data structure. Maybe you can provide some sample data rows. Furthermore you have to read something about writing functions in R (http://www.ats.ucla.edu/stat/r/library/intro_function.htm). Concentrate on the difference between local and global variables. Avoid access to variables inside a function, which are not defined inside or pass as a parameter. – FlorianSchunke May 16 '16 at 07:05
  • These are the headers in the data base: batting team, Batter, Match ID, Bowler, RunsBatsman, RunsExtra, RunsTotal, etc... the rest of the variables is not relevant to the question i have been using batter - to compare the batsmen names in the outerloop and RunsBatsmen in the inner loop to see if the batsmen has made 4 runs. if the two (name == name and runs = 4) then i increase the counter..and do this for every batsmen... – Simranjeet Narula May 16 '16 at 07:10
  • There are some more unknown variables and fields: `bat_name` and `numfours`. I can create sample data but fail at the second command: `fours = data.frame(bat_name)`, because `bat_name` is unknown... – FlorianSchunke May 16 '16 at 07:38

0 Answers0