r - 按变化计数汇总数据?
问题描述
我正在尝试汇总我的数据以说明教师在他们的日程安排中的不同课程数量。
基本上我的数据如下所示:
Id | Subject
123| algebra
123| geometry
123| algebra II
456| calc
456| calc
789| geometry
789| geometry
789| calc
and I need it to look like this:
Id | Subject count
123| 3
456| 1
789| 2
I have no idea where to start because I don't want it to simply count the number of courses they teach, I want the DIFFERENT courses. Please help!
解决方案
我们可以按“Id”分组并获得“主题”的不同n_distinct
计数summarise
library(dplyr)
df1 %>%
group_by(Id) %>%
summarise(Subject_Count = n_distinct(Subject))
# A tibble: 3 x 2
# Id Subject_Count
# <int> <int>
#1 123 3
#2 456 1
#3 789 2
或者使用data.table
, 转换为data.table
( setDT(df1)
),按“Id”分组,得到不同的计数uniqueN
library(data.table)
setDT(df1)[,.(Subject_Count = uniqueN(Subject)), by = Id]
数据
df1 <- structure(list(Id = c(123L, 123L, 123L, 456L, 456L, 789L, 789L,
789L), Subject = c("algebra", "geometry", "algebra II", "calc",
"calc", "geometry", "geometry", "calc")), class = "data.frame",
row.names = c(NA,
-8L))
推荐阅读
- laravel - Laravel 5.7:注销时是否应该删除“记住”cookie?
- excel - 当时在同一张工作表上向下钻取 3 个单独的 OLAP 数据透视表
- java - Consumer.endOffsets 如何在 Kafka 中工作?
- javascript - 将链接附加到使用 HTML 中的 JS 数组创建的表格的“URL”单元格
- javascript - 图表:彼此下方的两个双极条
- c# - 为什么我的炮塔停止射击 AI 播放器?
- java - Java - 在遍历对象列表时搜索特定属性
- c++ - 在 c++ 的结构构造函数中将变量作为引用传递
- python - 遍历 Excel 行并根据满足条件删除的最佳方法
- java - 避免不在 FX 应用程序线程上导致 UI 崩溃