r - 合并相同的日期
问题描述
我有这种格式的数据
data.frame(id = c(1,1,2),
date = c("2008-08-04 05:45:07","2008-08-04 09:45:07","2008-08-04 05:45:07"),
text = c("stg","another","final"))
# id date text
# 1 1 2008-08-04 05:45:07 stg
# 2 1 2008-08-04 09:45:07 another
# 3 2 2008-08-04 05:45:07 final
如何将具有相同日期的行合并为具有相同 ID 的行。预期输出示例:
data.frame(id = c(1,2),
date = c("2008-08-04", "2008-08-04"),
text = c("stg another","final"))
# id date text
# 1 1 2008-08-04 stg another
# 2 2 2008-08-04 final
解决方案
您可以尝试aggregate
如下
dfout <- aggregate(text ~ date + id, df, paste)
这样
> dfout
id date text
1 1 2008-08-04 05:45:07 stg, final
2 2 2008-08-04 09:45:07 another
推荐阅读
- angular - 如何在 Angular 9 中导入所有 Angular Material 模块
- reactjs - 在 React 中单击“将图像另存为”
- c++ - 在二叉搜索树 C++ 中计算平均值
- c# - S3“GetBucketLocation”REST API 返回不完整的结果
- sql - 如何将查询附加到具有其他 id 值的列值
- css - 简化长 CSS 选择器
- node.js - Express.js 大标头大小 400 错误请求
- makefile - make: *** 没有使目标“distclean”的规则。停止
- python - 将车牌图像扭曲为正面平行
- android - 如何更改统一 apk 的应用启动器图标