4

Possible Duplicate:
R - remove rows with NAs in data.frame

I have a data frame derived from the following function:

complete <- function(directory,id = 1:332) {

   csvfiles <- sprintf("/Users/myname/Desktop/%s/%03d.csv", directory, id)

   nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))

        rowlabels <- nrow(nrows)

        data.frame(id=sprintf('%3d', id), 
            nobs=sapply(csvfiles,function(x) length(count.fields(x))),
            row.names=rowlabels
           )
       }

This function counts the number of rows in each file contained within the directory produced by the object csvfiles. It then outputs a data frame showing the file number along with the count of rows (so two columns)

Thought I had it but problem is that I must now exclude rows within each file where an instance of NA exists.

How would I edit this to ignore those rows in each file and just count the rows where no NAs exist?

Community
  • 1
  • 1
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • 4
    This is your N-th post here with R-project in the title. That is redundant. Just use the `[r]` tags as you've done. And feel free to read some other posts. – Dirk Eddelbuettel Jan 16 '13 at 23:13
  • 4
    Have a look at `?complete.cases`. Having done this same online course a few months back, I'm pretty sure it gets mentioned in the lectures at some point, keep an ear out for hints like that. – Marius Jan 16 '13 at 23:15
  • 2
    Please search before posting again. Two minutes on google or the s.o. search would have found your answer. – thelatemail Jan 16 '13 at 23:27
  • 3
    Your response to @thelatemail places a big, blinking neon sign above your head that screams "I did not read the question shown to me, nor have I bothered to read the documentation on the function in question." – joran Jan 16 '13 at 23:45
  • 1
    This is not a duplicate. The OP is not asking how to remove rows, he's asking how to count complete rows. And yes, this is a "homework question." – Raydot Jul 21 '17 at 16:23

1 Answers1

5

Replace this line:

nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))

with this line, which uses the complete.cases function:

nrows <- sapply( csvfiles, function(f) nrow(complete.cases(read.csv(f))))

complete.cases takes a data frame and returns a data frame with the same columns, but with all the rows which contain at least one NA thrown out.

Jonathan Christensen
  • 3,756
  • 1
  • 19
  • 16