Using as.numeric and lenght

Hi,

Here is a way of testing the different columns for missing or 0 values

#Dummy data
myData = data.frame(x = 1:10, 
                    y = c(1,5,0.2,0,0,8,NA,5,NA,6.3),
                    z = c(1,NA,0.2,7,0,8,4.3,5,0,6.3))

#Number of values in columns that are missing or 0
nMissing = apply(myData, 2, function(x){
  sum(is.na(x) | x == 0)
})
nMissing
#> x y z 
#> 0 4 3

#Number of 'correct' values in each column 
nrow(myData) - nMissing
#>  x  y  z 
#> 10  6  7

Created on 2021-08-26 by the reprex package (v2.0.1)

The apply function runs a function over each column (option 2 as second argument), in this case a check of NA or 0

Since I could not work with your data or code, I came up with a dummy example. Next time consider creating a reprex. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Hope this helps,
PJ

1 Like