title: How to use 'sapply' or 'lapply' to count the number of lines in n csv files?
body: <p>I need to write a function that takes 2 arguments: <code>directory</code> and <code>id</code>. <code>directory</code> is essentially the working directly of R. <code>id</code> is the name of the file. All files have extension <code>csv</code> and a name between 001 to 332.</p>
  
  <p>The function should return a data frame with two columns: <code>id</code> and <code>nrow</code>. <code>id</code> is the name of the file, and <code>nrow</code> is the number of rows in that file.</p>
  
  <p>I started with the following codes but this will only work with if the there is only 1 <code>id</code> get passed to the function:</p>
  
  <pre><code>directory = 'specdata'
  id = 1 # this should be able to take a list of numbers i.e. 1:332
  id1 = if(nchar(id) == 1) {paste("00",id,sep="")}
        else if (nchar()== 2) {paste("0",id,sep="")})
  file = paste(directory,"/",as.character(id1),".csv", sep="")
  data = read.csv(file)
  casenum = nrow(data)
  output = c(id1, casenum)
  </code></pre>
  
  <p>How can I modify the codes so that the function can repeat itself if more than 1 <code>id</code> is passed. For example the line <code>id = c(1,2,3,5,6)</code> is getting passed?
  I am thinking of using <code>lapply</code> or <code>sapply</code> but don’t know where to start.
  Thanks,</p>