Frequency of values per column in table

守給你的承諾、 提交于 2019-11-29 15:22:53
JasonWang
library(dplyr)
library(reshape2)
df %>%
  melt() %>%
  dcast(value ~ variable, fun.aggregate=length)

#   value a b c d
# 1     1 2 0 2 1
# 2     2 1 4 2 0
# 3     3 2 2 0 6
# 4     4 0 1 2 0
# 5     5 2 0 1 0

Data

df <- structure(list(a = c(1L, 1L, 2L, 3L, 3L, 5L, 5L), b = c(2L, 2L, 
2L, 2L, 3L, 3L, 4L), c = c(1L, 1L, 5L, 4L, 2L, 4L, 2L), d = c(3L, 
3L, 3L, 3L, 3L, 3L, 1L)), .Names = c("a", "b", "c", "d"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7"))

For the same data set that you provided in the question this would be another solution (base-R):

myfreq <- sapply(df, function(x) table(factor(x, levels=unique(unlist(df)), ordered=TRUE)))

Output would be:

> myfreq

#   a b c d 
# 1 2 0 2 1 
# 2 1 4 2 0 
# 3 2 2 0 6 
# 5 2 0 1 0 
# 4 0 1 2 0
library(tidyverse)

dt <- data.frame(a = c(1L, 1L, 2L, 3L, 3L, 5L, 5L), b = c(2L, 2L, 2L, 2L, 3L, 3L, 4L),
                 c = c(1L, 1L, 5L, 4L, 2L, 4L, 2L), d = c(3L, 3L, 3L, 3L, 3L, 3L, 1L))

dt2 <- dt %>%
  mutate(ID = 1:n()) %>%
  gather(Group, x, -ID) %>%
  select(-ID) %>%
  mutate(Group = paste(Group, "n", sep = "_")) %>%
  count(Group, x) %>%
  spread(Group, n, fill = 0L)

Using tabulate in base R:

apply(df,2,function(x) tabulate(x)[min(df):max(df)])

#     a  b c  d
#[1,] 2  0 2  1
#[2,] 1  4 2  0
#[3,] 2  2 0  6
#[4,] 0  1 2 NA
#[5,] 2 NA 1 NA
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!