## How to combine contents of two frequency tables into one frequency table in R?

I had a question about how to combine two frequency tables into one frequency table.

So if I have 2 tables:

table1: Col1 18 19 17 19 13 19 table2: Col1 18 19 12 15 18

I'd like to make a 3rd table, `table3`

such that `table3$"Col2"`

counts the number of times a number in `table3$"Col1"`

appears in `table1$"Col1"`

and such that `table3$"Col3"`

counts the number of times a number in `table3$"Col1"`

appears in `table2$"Col1"`

`table3$"Col1"`

is a list of all elements in `table1$"Col1"`

and in `table2$"Col2"`

table3: Col1 Col2 Col3 12 0 1 13 1 0 15 0 1 17 1 0 18 1 2 19 3 1

I originally tried doing this:
`table3$"Col1"<-table(table1$"Col1",table2$"Col1")`

but it doesn't work because `table1$"Col1"`

and `table2$"Col1"`

have different lengths:

`Error in table(table1$"Col1", table2$"Col1") : all arguments must have the same length`

Here's another option:

f <-function(x,y) sum(x %in% y) V1 <- sort(unique(c(table1$'Col1', table2$'Col1'))) V2 <- sapply(V1,f,x = Col1) V3 <- sapply(V1,f,x = Col2) > data.frame(V1,V2,V3) V1 V2 V3 1 12 0 1 2 13 1 0 3 15 0 1 4 17 1 0 5 18 1 2 6 19 3 1

**Combine frequency tables into a single data frame,** 2. Reduce. Here is a second solution. It uses Reduce in the core of R. This one adds names to the list and then extracts the frequencies, names and t1<-table(strsplit(tolower("this is a test in the event of a real word file you Video explaining how to generate a frequency table from a question that allows multiple answers. SPSS Frequency table multiple answer SPSS Tables - Frequency or Cross table of a Multiple

Here's another `dplyr`

solution.

First, I load the libraries.

library(dplyr) library(magrittr)

Next, I count each element in both tables using `table`

, then perform a full join. Missing elements in each table will appear as `NA`

.

df <- full_join(data.frame(table(table1)), data.frame(table(table2)), by = c("table1" = "table2"))

Finally, I replace `NA`

s with zeroes, rename the columns, and sort according to the first column.

df %<>% replace(is.na(.), 0) %>% rename_all(funs(paste("Col", 1:3, sep = ""))) %>% arrange(Col1) # Col1 Col2 Col3 # 1 12 0 1 # 2 13 1 0 # 3 15 0 1 # 4 17 1 0 # 5 18 1 2 # 6 19 3 1

**How can I add two (frequency tables) of different lengths? : rstats,** I have two frequency tables that I would like to combine, say What function should I use in order to combine these into one big table where the common I'm trying to tell R this: "make a new data set called 'New York' which just has the case Below are the steps to generate a frequency table of multiple variables that have the same values. A detailed instruction with screenshots can be downloaded here for version 23 or older, or here for version 24, or you can watch the video on the right. Click in the menubar on Analyze; Click on Tables (or in version 23 on Custom Tables)

I am going to use a `tidyverse`

solution. There is perhaps a base `R`

approach that could work as well.

`library(tidyverse)`

table1 <- read.table(text = " Col1 18 19 17 19 13 19", header = TRUE) table2 <- read.table(text = " Col1 18 19 12 15 18", header = TRUE)

First, we want to get a list of all possible options for the `Col1`

column of `table3`

.

table3 <- data.frame(Col1 = (unique(c(table1$Col1, table2$Col1))))

Then we use the `count`

function from `dplyr`

to get the number of instances of each observation in both `table1`

and `table2`

. Note, that `count`

returns a column called `n`

to represent the tally of each observation. I rename that to match the column names in your final `table3`

.

df1 <- table1 %>% count(Col1) %>% rename(Col2 = n) df2 <- table2 %>% count(Col1) %>% rename(Col3 = n)

Finally, we join all of these together with a `left_join`

and then replace missing values with 0.

table3 <- table3 %>% left_join(df1, by = "Col1") %>% left_join(df2, by = "Col1") %>% mutate(Col2 = ifelse(is.na(Col2), 0, Col2), Col3 = ifelse(is.na(Col3), 0, Col3)) %>% arrange(Col1) > table3 Col1 Col2 Col3 1 12 0 1 2 13 1 0 3 15 0 1 4 17 1 0 5 18 1 2 6 19 3 1

**My favourite R package for: frequency tables – Dabbling with Data,** E, 1. E, 2. B, 3. B, 4. B, 5. B, 6. C, 7 What would I like my 1-dimensional frequency table tool to do in an ideal world? Provide a count of how So what options come by default with base R? Join 72 other followers. Follow. 2. Manually combine 4 tables into one table 3. PivotTable to create Cross Tabulated Frequency Distribution 4. PivotTable to calculate Proportions for each year (% of Column Total) 5.

**[R] Joining two (or more) frequency tables,** Previous message: [R] Joining two (or more) frequency tables; Next table(z$father.id, z$value) 100 200 300 340 a 1 1 1 0 b 1 1 0 1 > On Wed, May head > around it when it comes to the pre-analysis data manipulation like SPSS Tutorial 8 - Combining Percentages and Frequency Counts in Cross-tabulations SPSS Grouped frequency table - Duration: 6:05. stikpet 96,265 views. 6:05. SPSS - Summarizing Two Categorical

**Introduction to summarytools,** Doing so has the following effects on the resulting table: To generate frequency tables for all variables in a data frame, no need to Using the tobacco data frame, we'll cross-tabulate the two categorical variables smoker and diseased. print(ctable(x = tobacco$smoker, y = tobacco$diseased, prop = "r"), I have 15 tables, with the same column names , but different row values in each table. how to combine these tables, so I can see all the row values in one single table. Message 1 of 18. Re: how to merge multiple tables. Subscribe to RSS Feed. Email to a Friend. Report Inappropriate Content. 07-22-2018 12:28 PM.

**Combining flattened contingency tables (and other tables),** Sex <- xtabs(Freq~Survived+Class+Sex,data=Titanic) tab.Survived. We can even add a simple table of counts: Of course, it is not enough to see such tables on screen, we also may need them in a presentable format, such as LaTeX or HTML. 2. Female, 26 . 8, 73 . 2. Class, 1st, 37 . 5, 62 . 5. 2nd, 58 . 6, 41 . 4. 3rd, 74 . Video explaining how to generate a frequency table from a list of questions that use the same scale (including Likert scales) How to insert images into word document table SPSS Tables