Read data frame rows that do not contain `NA`

Possible duplicate:
R - delete lines with NA in the data.frame file

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 lines in each file contained in the directory created by the csvfiles object. Then it displays a data frame showing the file number along with the number of rows (so there are two columns)

Thought I had it, but the problem is that now I have to exclude lines in every file where an NA instance exists.

How do I edit this to ignore these lines in every file and just count lines where there is no NA ?

+4
source share
1 answer

Replace this line:

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

with this line that uses the complete.cases function:

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

complete.cases accepts a data frame and returns a data frame with the same columns, but with all rows containing at least one NA .

+4
source

All Articles