Using table () to create 3 variable frequency tables in R

I am new to R and was looking for some help. I understand that the following problem is quite simple and is looking for similar questions. No one gives quite the answer I'm looking for - any help would be appreciated.

Problem:

Creating a frequency table using the table() function for three variables with data in the format:

  Var1 Var2 Var3 1 0 1 0 2 0 1 0 3 1 1 1 4 0 0 1 

Where, 0 = "No" and 1 = "Yes"

And the summary table is in the following format with variables and values ​​indicated:

  Var3 Yes No Var1 Yes 1 0 No 1 2 Var2 Yes 1 2 No 1 0 

What I have tried so far:

Using the following code, I can create a variable table 2 with labels for variables, but not for values ​​(i.e. No and Yes).

 table(data$Var1, data$Var3, dnn = c("Var1", "Var3")) 

It looks like this:

  Var3 Var1 0 1 0 2 1 1 0 1 

When I try to mark the values ​​of rows and columns (0 = No and 1 = Yes), I understand that row.names and responseName can be used, however, the following attempt to label row names gives an error all arguments must have the same length .

 > table(data$Var1, data$Var2, dnn = c("Var1", "Var2"), row.names = c("No", "Yes")) 

I also tried using ftable() , however the table shape created using the code below is incorrect, which leads to the wrong frequencies for the problem. The problem with marking row and column values ​​remains.

 > ftable(data$Var1, data$Var2, data$Var3, dnn = c("Var1", "Var2", "Var3")) Var3 0 1 Var1 Var2 0 0 0 1 1 2 0 1 0 0 0 1 0 1 

Any help on using table() to get the table of the desired shape would be greatly appreciated.

+6
source share
2 answers

You can try tabular from library(tables) after changing labels, as shown on @thelatemail page

 library(tables) data[] <- lapply(data, factor, levels=1:0, labels=c('Yes', 'No')) tabular(Var1+Var2~Var3, data=data) # Var3 # Yes No #Var1 Yes 1 0 # No 1 2 #Var2 Yes 1 2 # No 1 0 

data

 data <- structure(list(Var1 = c(0L, 0L, 1L, 0L), Var2 = c(1L, 1L, 1L, 0L), Var3 = c(0L, 0L, 1L, 1L)), .Names = c("Var1", "Var2", "Var3" ), class = "data.frame", row.names = c("1", "2", "3", "4")) 
+4
source

The easiest way is to use the reshape2 package. First, you will need to convert numerical information into factors so that it does not treat it as a number.

 data$Var1 <- as.factor(data$Var1) data$Var2 <- as.factor(data$Var2) data$Var3 <- as.factor(data$Var3) 

Then you can simply apply table(data) to get the information you need. If you really want to convert it in the format you specify, then pull it as data.frame , and then convert it as needed:

 df <- as.data.frame(table(data)) library(reshape2) dcast(df, Var1+Var2 ~ Var3) 

This is the result:

  Var1 Var2 0 1 1 0 0 0 1 2 0 1 2 0 3 1 0 0 0 4 1 1 0 1 

EDIT . You can simply use ftable in a data frame after all its factors:

 > ftable(data) Var3 0 1 Var1 Var2 0 0 0 1 1 2 0 1 0 0 0 1 0 1 
+1
source

Source: https://habr.com/ru/post/987845/


All Articles