R中按日期计算唯一值的数量

编程入门 行业动态 更新时间:2024-10-10 01:27:36
本文介绍了R中按日期计算唯一值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请帮我计算每个日期的唯一 ID 数.所以,最初,有这个 ID 和日期的数据框

Please help me to count the number of unique IDs per Date. so, initially, there is this data frame of IDs and dates

ID Date 1 2009/11/1 1 2009/11/2 1 2009/11/2 2 2009/11/1 2 2009/11/1 2 2009/11/2 3 2009/11/1 3 2009/11/3

可以按日期重新排列.如果我们这样做,那么我们将看到在 1 日有 3 个唯一 ID.在第 2 个唯一 ID 和第 3 个唯一 ID 上.所以决赛桌应该是这样的:

It is possible to rearrange it by date. If we do so then we will see that on the 1st there are 3 unique IDs. On the 2ed 2 unique ID and on the 3rd there is one unique ID. So the final table should look like this:

Date uniqueIDs 2009/11/1 3 2009/11/2 2 2009/11/3 1

我知道,如果值为1"或0",则可以使用 sum 与 aggregate 进行聚合:

I know that it is possible to aggregate with aggregate by using sum if the value is '1' or '0 'like that:

aggregate(DataFrame$RoomAv ~ DataFrame$Date, DataFrame, sum)

但是如何计算每天唯一的 ID 数量?ID 列是整数列.

But how to count the unique number of IDs per day? The ID column is an integer column.

非常感谢!

推荐答案

这是 sqldf 的解决方案.

library(sqldf) rawData <-"ID,Date 1,2009/11/1 1,2009/11/2 1,2009/11/2 2,2009/11/1 2,2009/11/1 2,2009/11/2 3,2009/11/1 3,2009/11/3 " data <- read.csv(text = rawData,as.is=TRUE) sqlStmt <- "select Date, count(distinct ID) from data group by Date" sqldf(sqlStmt)

...和输出:

> sqldf(sqlStmt) Date count(distinct ID) 1 2009/11/1 3 2 2009/11/2 2 3 2009/11/3 1 >

更多推荐

R中按日期计算唯一值的数量

本文发布于:2023-11-22 02:11:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   按日   唯一值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!