LaVOZs

The World’s Largest Online Community for Developers

'; r - Error in method to visualize missing values - LavOzs.Com

I am trying to visualize missing values (NA) in a data.frame containing the Hepatitis dataset and using the package VIM. I am trying to do that by using the function spineMiss:

spineMiss(hepatitis[, c("PRONOSTICO", "PROTIME")])

But I get the following error:

Error in createPlot(main, sub, xlab, ylab, labels): (list) object cannot be coerced to type 'double'

This the str of the dataframe:

enter image description here

If I use the function like this:

a <- hepatitis$PRONOSTICO
b <- hepatitis$PROTIME
spineMiss(c(a,b))

I do not get any error, but the result does not make much sense. What I am doing wrong?

The data in the documentation is working correctly

library(VIM)
data(tao, package = "VIM")
## for missing values
spineMiss(tao[, c("Air.Temp", "Humidity")])

and both the columns are numeric

str(tao[, c("Air.Temp", "Humidity")])
#'data.frame':  736 obs. of  2 variables:
# $ Air.Temp: num  27.1 27 27 26.9 26.8 ...
# $ Humidity: num  79.6 75.8 76.5 76.2 76.4 76.7 76.5 78.3 78.6 76.9 ...

As one of the column is factor, it could be used as the first variable. It is not giving any errors

set.seed(24)
hepatitis <- data.frame(PROTIME = sample(c(NA, 80:95), 100,
  replace = TRUE), PRONOSTICO = sample(c("FALLECE", "VIVE"), 
 100, replace = TRUE))

spineMiss(hepatitis[c("PRONOSTICO", "PROTIME")])

I got the conclusion that the error was related with the structure of the data.frame itself. It was not created by me with R code but read from a file. After the reading I have done some preprocessing (type conversion, etc). The str(hepatitis) shows that the original specification of the attributes of the data.frame are still stored in it. I do not understand completely how that makes spineMiss malfunction but certainly that is the root of the problem.

I have solved it by creating a new data.frame and copying all the data of the previous one (the read from a file and preprocessed) in it:

aux <- data.frame(matrix(ncol = 20, nrow = 0))
colnames(aux) <- atributos
for(i in 1:nrow(hepatitis)){
  aux[i,] = hepatitis[i,]
}
spineMiss(aux[, c("PRONOSTICO", "PROTIME")])
Related
Remove rows with all or some NAs (missing values) in data.frame
How do I replace NA values with zeros in an R dataframe?
Use grepl and ifelse to add a new column
Not getting Linear Regression Plot
error with interpSpline function
How to convert Python Pandas Dataframe to R data.frame
How to subset variables which have NA in the values?