I have a large data set with 11 columns and 100000 rows (for example) in which i have values 1,2,3,4. Where 4 is a missing value. What i need is to compute the Mode. I am using following data and function
ac<-matrix(c("4","4","4","4","4","4","4","3","3","4","4"), nrow=1, ncol=11)
m<-as.matrix(apply(ac, 1, Mode))
if i use the above command then it will give me “4” as the Mode, which i do not need. I want that the Mode will omit 4 and display “3” as Mode, because 4 is a missing value.
Thanks in advance.
R has a powerful mechanism to work with missing values. You can represent a missing value with
NAand many of the R functions have support for dealing withNAvalues.Create a small matrix with random numbers:
Since you represent missingness by the value 4, you can replace each occurrence by
NA:To calculate, for example, the mean:
To calculate the mode, you can use the function
Modein packageprettyR: (Note that in this very small set of data, only the 4th row has a unique modal value: