Adrian Gasiński
Adrian Gasiński

Reputation: 33

R - complete.cases not all arguments have the same length

I have problem with R complete.cases() funciton.

I am using Electric power consumption data and I wanted to check if there are any NAs in my subset using complete.cases() function.

I expect to get number of complete cases but instead I get an error saying that "not all arguments have the same legnth".

I give complete.cases() only one argument that is data frame. All columns in df have the same length. Of course I can check NAs in every column using sum(is.na()) funciton but I am curious why complete.cases() doesn't work.

Moreover when I generated data frame with 3 columns filled by random numbers complete.cases() worked.

Here is my code so that you can reproduce error:

### READING DATA

# reading full file
data <- read.table("household_power_consumption.txt", header=1, sep=";", na.strings="?")

# changing Date and Time columns to R classes
data$Time = strptime(paste(data$Date, data$Time),"%d/%m/%Y %H:%M:%OS")
data$Date = as.Date(data$Date, format="%d/%m/%Y")

# filtering to needed days
data = subset(data, Date == '2007-02-01' | Date == '2007-02-02')

# checking if there are any NAs in data
dim(data)
sum(complete.cases(data))

Upvotes: 2

Views: 18451

Answers (1)

demonplus
demonplus

Reputation: 5801

There is some problem with complete.cases and handling dates, it is mentioned here:

R apply error - error in as.matrix.data.frame()

Your data frame contains dates so it should be related to that. I checked that na.omit works fine with your dataset so you can use it instead.

Upvotes: 3

Related Questions