- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create NA column for a contingency table in R?
To create NA column for a contingency table in R, we can follow the below steps −
- First of all, create a data frame with two columns having some NA values.
- Create a contingency table for two columns.
- Create the same table using useNA.
Create the data frame
Let's create a data frame as shown below −
x<-sample(c(NA,1,2,3,4),25,replace=TRUE) y<-sample(c(NA,10,12,13,15),25,replace=TRUE) df<-data.frame(x,y) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y 1 4 15 2 NA NA 3 NA NA 4 NA 12 5 3 NA 6 2 15 7 4 NA 8 2 NA 9 3 NA 10 1 13 11 1 NA 12 NA 10 13 NA 15 14 4 10 15 3 15 16 2 15 17 3 NA 18 2 10 19 3 13 20 2 12 21 3 10 22 1 13 23 4 12 24 2 13 25 3 15
Create a contingency table
Use table function to create the contingency table for data in df −
x<-sample(c(NA,1,2,3,4),25,replace=TRUE) y<-sample(c(NA,10,12,13,15),25,replace=TRUE) df<-data.frame(x,y) table(df$x,df$y)
Output
10 12 13 15 1 0 0 2 0 2 1 1 1 2 3 1 0 1 2 4 1 1 0 1
Create a contingency table with NA
Use table function to create the contingency table for data in df with useNA argument −
x<-sample(c(NA,1,2,3,4),25,replace=TRUE) y<-sample(c(NA,10,12,13,15),25,replace=TRUE) df<-data.frame(x,y) table(df$x,df$y,useNA="always")
Output
10 12 13 15 1 0 0 2 0 2 1 1 1 2 3 1 0 1 2 4 1 1 0 1 1 1 0 1 2
Advertisements