I need to count the NAs elements in a CSV file containing numeric values and NAs. Using read.csv, r automatically reads them all as character, therefore it's impossibile to detect how many NAs there are. How can i fix that?
I need to count the NAs elements in a CSV file containing numeric values and NAs. Using read.csv, r automatically reads them all as character, therefore it's impossibile to detect how many NAs there are. How can i fix that?
An approach using which:
x = c(1,2,3)
y = c(6,5,4)
z = c(NA, 8, NA)
df1 <- data.frame(x,y,z)
# which has arr.ind option that returns locations by row/column
NA_idx <- which(is.na(df1) == TRUE, arr.ind = TRUE)
NA_idx
row col
[1,] 1 3
[2,] 3 3
This may not be so directly useful to the 'how many of x', 'how many of NA', so perhaps
length(which(is.na(unlist(df1)==TRUE)))
[1] 2
length(unlist(df1))
[1] 9
length(unlist(df1)) - length(which(is.na(unlist(df1)==TRUE)))
[1] 7