r - R重塑数据框以获得观察的出现总数
问题描述
我正在考虑如何像这样重塑数据框:
id type points times
1 A 3 1
2 B 3 2
3 A 3 3
4 B 2 4
5 A 1 5
对此:
points A B
1 5 0
2 0 4
3 4 2
所以,我想让点和类型成为列,并统计一个点在所有类型中出现的总数。
解决方案
你可能会dcast
使用reshape2
reshape2::dcast(dat[-1], points ~ type, fill = 0, fun.aggregate = sum)
# points A B
#1 1 5 0
#2 2 0 4
#3 3 4 2
或者没有外部软件包,您可以使用xtabs
xtabs(times ~ points + type, data = dat)
# type
#points A B
# 1 5 0
# 2 0 4
# 3 4 2
推荐阅读
- sql - 在什么用例中您会使用 CTE 而不是临时表
- python - 在 Databricks 群集上从 Azure Artefact 点安装 Python 包
- javascript - 带有分页的Vue js画廊图像
- python-3.x - Python - 如何将多行写入文件?(列表,不是字符串)
- javascript - 为什么当它的处理程序返回一个数组时 jest mockImplementation 不起作用?
- c - 没有“=”的 K&R C 初始化在 gcc 中查找支持的选项
- android-studio - 导入项目时无法解析符号以及库树上缺少 Androidmanifest.xml 文件
- kendo-ui - 避免在 Kendo 对话框动作中创建闭包
- opencv - 将包含 jpeg 图像的文件夹转换为 hdf5
- text - MLCK在文本分析中是什么意思