Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting occurencies in every column in R

Tags:

dataframe

r

Hello I need to count the occurencies of every number in each column. Example data-frame:

A   B   C
2   1   2
2   1   1
1   1   3
3   3   3
3   2   2
2   1   2

I want my output to look like this

how_much  A   B   C
1         1   4   1
2         3   1   3
3         2   1   2
like image 728
Frontmaniaac Avatar asked Dec 04 '22 17:12

Frontmaniaac


1 Answers

In tidyverse you could do:

library(tidyverse)

gather(df1) %>%
  group_by(key,value) %>%
  count() %>%
  pivot_wider(value, names_from = key, values_from = n, values_fill = 0)

value     A     B     C
  <int> <int> <int> <int>
1     1     1     4     1
2     2     3     1     3
3     3     2     1     2
like image 155
KU99 Avatar answered Dec 12 '22 13:12

KU99