Number of matched items in columns with NA values in R
I am working in R and I have a matrix with values "A", "B" and "NA" and I would like to count the number of values "A" or "B" or NA in each column.
sum (MYDATA [, i] == "A") and sum (MYDATA [, i] == "B") worked fine for columns without NA. For columns containing NA, it is possible to count the number of NA with the sum (is.na (mydata [, i]) but in those columns the sum (mydata [, i] == "A") returns NA as result instead of number. How can I to count the number of "A" values in columns containing NA values?
Thank you for your help!
Example:
> mydata
V1 V2 V3 V4
V2 "A" "A" "A" "A"
V3 "A" "A" "A" "A"
V4 "B" "B" NA NA
V5 "A" "A" "A" "A"
V6 "B" "A" "A" "A"
V7 "B" "A" "A" "A"
V8 "A" "A" "A" "A"
> sum(mydata[,2]=="A")
[1] 6
> sum(mydata[,3]=="A")
[1] NA
> sum(is.na(mydata[,3]))
[1] 1
+3
source to share
6 answers
To expand on the answer from @Andrie,
mydata <- matrix(c(rep("A", 8), rep("B", 2), rep(NA, 2), rep("A", 4),
rep(c("B", "A", "A", "A"), 2), rep("A", 4)), ncol = 4, byrow = TRUE)
myFun <- function(x) {
data.frame(n.A = sum(x == "A", na.rm = TRUE), n.B = sum(x == "B",
na.rm = TRUE), n.NA = sum(is.na(x)))
}
apply(mydata, 2, myFun)
0
source to share