简体   繁体   中英

Count empty rows in a data.frame containing character, Date and numeric

Hei,

I have a data.frame with different columns, some are integer, some Date, some numeric and even some are character. The data frame is huge and contains besides some identifier columns, data columns. Of course some rows within the data columns are empty. Empty is a row, if integer, numeric are NA, Date Columns is < cNA > , and Character Columns are empty (=="").

How can I count these empty sub-columns of the whole Dataset?

foo <- data.frame(ID=c(1:4),
              character=c("a", "b","","d"), 
              integer=c(1, 2,NA,4),
              numeric=c(1.1, 2.2,NA,4.4),
              Date=as.Date(c("2015-11-10","2015-11-10","","2015-11-10")))
unlist(lapply(foo,class)) # correct classes of columns 

In this example row3 is empty. Is there a possiblity to identify it? I tried to count the NAs:

foo$emptyrows<-rowSums(is.na(foo[,2:5]))

But the empty Character is clearly not NA. Then I tried to replace the empty character:

foo[foo==""]<-NA

But this leads to an error because of the Date-Column. Then I tried:

foo2 <- data.frame(apply(foo,1:2,
                   function(x) if( x %in% c('',' ')) return(NA) else return(x)))

But this converts all columns to factors. This is not useful for further data processing.

unlist(lapply(foo2,class))

The last step is this question! Thank you for any help!

Der Elch von Oslo

sapply(foo,function(x) table(as.character(x) =="")["TRUE"])

This gives you NA(if the column doesn't have empty fields,else the number of empty values.

Unlike the above solution, you need not convert empty strings to NA values.

Here's one of doing this. In this case, we are using as.character in our comparison:

foo[sapply(foo, function(x) as.character(x)=="")] <- NA

Then you can do a filter based on the rowSums, depending on how many NAs are allowed (here I chose 1, to mean any NA would kick out the row):

foo[rowSums(is.na(foo)) < 1, ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM