## performing frequency table by group with calculation count of value in R

r frequency table multiple variables

frequency function in r

frequency table in r

r count string frequency

count factors in r

grouped frequency table r

how to export a frequency table in r

Suppose this is my dataset

(dput) dataset<-structure(list(group1 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("b", "x"), class = "factor"), group2 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("g", "y"), class = "factor"), var1 = c(2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L)), .Names = c("group1", "group2", "var1"), class = "data.frame", row.names = c(NA, -9L ))

I need calculate frequency for two groups

x+y b+g

and for variable var1, calculate count of 1 value and 2 value. For each group. So the desired output

total_count_of_group var1-1 var1-2 x y 5 3 2 b g 4 2 2

This output mean that total_count_of_group x+y=5 obs. by this group. where 1 value meets 3 times and 2 value two times.

similary total_count_of_group b+g=4 obs. by this group. where 1 value meets 2 times and 2 value two times.

How to get such table?

This can be solved in two steps:

- Aggregate group totals and update
`dataset`

- Reshape from long to wide format

using `data.table`

:

library(data.table) dcast(setDT(dataset)[, total_count_of_group := .N, by =. (group1, group2)], group1 + group2 + total_count_of_group~ paste0("var1=", var1), length)

group1 group2 total_count_of_group var1_1 var1_2 1: b g 4 2 2 2: x y 5 3 2

Please, note that this will work for an arbitrary number of different values in `var1`

as well as an arbitrary number of groups.

**How to Get the Frequency Table of a Categorical Variable as a Data ,** I recently needed to get a frequency table of… What are the possible values of “gear”? The table() function in Base R does give the counts of a categorical variable, If you don't already have the “plyr” package, install it first by running the command It also will take a formula as the second parameter. How can I, in R calculate the overall variance and the variance for each group from a dataset that looks like this (for example): Group Count Value A 3 5 A 2 8 B 1 11 B

You could produce three tables, select the relevant counts, and then combine in a dataframe.

a <- table(dataset$group1, dataset$group2) b <- table(dataset$var1[dataset$group1=='x']) d <- table(dataset$var1[dataset$group1=='b']) data.frame(total_count_of_group = c(a[2,2], a[1,1]), var1_1 = c(b[1], b[2]), var1_2 = c(d[1], d[2])) total_count_of_group var1_1 var1_2 1 5 3 2 2 4 2 2

**My favourite R package for: frequency tables – Dabbling with Data,** Especially when my definition of frequency tables here will restrict itself to library(epiDisplay) tab1(data$Type, sort.group = "decreasing", Counts, percentages, cumulative percentages, missing values data, yes, all here! remove any particular categories from analysis before performing the calculations I want to get for each group the frequencies of the values of a factor/categorial variable. Frequency table by group in data.table (R) [duplicate] Faster ways

library(tidyverse) dataset %>% group_by(group1, group2) %>% # for each combination of groups mutate(counts = n()) %>% # count number of rows count(group1, group2, var1, counts) %>% # count unique combinations spread(var1, n, sep = "_") %>% # reshape dataset ungroup() # forget the grouping # # A tibble: 2 x 5 # group1 group2 counts var1_1 var1_2 # <fct> <fct> <int> <int> <int> # 1 b g 4 2 2 # 2 x y 5 3 2

**Frequencies,** Learn how to use R to create frequency and contingency tables from category in counts, include the table option exclude=NULL if the variable is a vector. By default, the p-value is calculated from the asymptotic chi-squared distribution of the Use the mantelhaen.test(x) function to perform a Cochran-Mantel-Haenszel The plyr package is very useful when it comes to categorical data. As we see, the count () function is really flexible and can generate the Data Frame we want. It is now easy to add the frequency of the categorical data to the original Data Frame x. The table () function is really useful as a quick summary and, with a little work, can produce

Here is an option using `base R`

out <- aggregate(cbind(var = rep(1, nrow(df1))) ~ ., transform(df1, counts = ave(var1, group1, group2, FUN = length)), length) reshape(out, idvar = c('group1', 'group2', 'counts'), timevar= 'var1', direction= 'wide') # group1 group2 counts var.1 var.2 #1 b g 4 2 2 #3 x y 5 3 2

**The advantages of using count() to get N-way frequency tables as ,** Want to share your content on R-bloggers? click here if you have a blog, tables in R. Several commenters provided alternative ways of doing so, To get a 2-way frequency table (i.e. a frequency table of the counts of a You can use the row and column indices of this object to extract a particular value. I am working with the 'mtcars' data-set: mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Here's a `tidyverse`

solution :

library(tidyverse) dataset %>% group_by(group1, group2) %>% summarize(total = n(), x = list(table(var1) %>% as_tibble %>% spread(var1,n))) %>% unnest # # A tibble: 2 x 5 # # Groups: group1 [2] # group1 group2 total `1` `2` # <fct> <fct> <int> <int> <int> # 1 b g 4 2 2 # 2 x y 5 3 2

**How to get a frequency count based on two columns (variables) in ,** How do I get a frequency count based on two columns (variables) in an R dataframe? Freqs <- table(df.example$Group, df.example$Size). Freqs. Work with “kable” from the Knitr package, or similar table output tools. I often use R markdown and would like the ability to show the frequency table output in reasonably presentable manner. Have a sensible set of defaults (aka facilitate my laziness). So what options come by default with base R? Most famously, perhaps the “table” command.

**Count/tally observations by group,** Source: R/count-tally.R. tally. If the data is already grouped, count() adds an additional group that is removed afterwards. add_tally() adds a column n to a table based on the number of items within each existing group, while If specified, will perform a "weighted" tally by summing the (non-missing) values of variable wt . I recently needed to get a frequency table of a categorical variable in R, and I wanted the output as a data table that I can access and manipulate. This is a fairly simple and common task in statistics and data analysis, so I thought that there must be a function in Base R that can easily generate this.

**Basic Statistical Analysis Using the R Statistical Package,** In R, these values can be represented as a column vector (as a data set, these values would The following example creates an age group variable that takes on the value 1 for those 1.8 Finding frequencies and proportions for categorical variables 2.1.6.2 Inputting counts from a 2x2 table into R for calculation of a RR. Use with group_by(). nth observation of the group : Count : n() Use with group_by(). Count the number of rows : n_distinct() Use with group_by(). Count the number of distinct observations

**Group operations in R,** Group operations in R written March 27, 2016 in r,programming tips. In order to make the categories clearer when performing the group One of the most basic group operations are frequency tables, which give us the count of the values in table to proportions by encasing it in the function prop.table . Then there's the "table" of data created by the view, which is dependent on the pills on Columns, Rows, Pages, and Marks Card, where the data values are determined by the dimensions (they create GROUP BY's in the query that Tableau issues to the data source) and aggregations used for the measures.