如何查找与数据框中的每个日期对应的唯一ID数

时间:2015-10-21 04:59:51

标签: r for-loop dataframe unique

我有一个如下所示的数据框:

      date         time              id            datetime    
1 2015-01-02 14:27:22.130 999000000007628 2015-01-02 14:27:22 
2 2015-01-02 14:41:27.720 989001002807730 2015-01-02 14:41:27 
3 2015-01-02 14:41:27.940 989001002807730 2015-01-02 14:41:27 
4 2015-01-02 14:41:28.140 989001002807730 2015-01-02 14:41:28 
5 2015-01-02 14:41:28.170 989001002807730 2015-01-02 14:41:28 
6 2015-01-02 14:41:28.350 989001002807730 2015-01-02 14:41:28 

我需要找到该数据框中每个“日期”的唯一“id”的数量。

我试过了:

sums<-data.frame(date=unique(data$date), numIDs=0)

for(i in unique(data$date)){
  sums[sums$date==i,]$numIDs<-length(unique(data[data$date==i,]$id))
}

我收到以下错误:

 Error in `$<-.data.frame`(`*tmp*`, "numIDs", value = 0L) : 
   replacement has 1 row, data has 0
 In addition: Warning message:
 In `==.default`(data$date, i) :
   longer object length is not a multiple of shorter object length

任何想法?谢谢!

希望这有帮助!

data <- structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), mon = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), year = c(115L, 115L, 115L, 115L, 
    115L, 115L, 115L, 115L, 115L, 115L), wday = c(5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L), yday = c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), zone = c("PST", "PST", "PST", "PST", "PST", 
    "PST", "PST", "PST", "PST", "PST"), gmtoff = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), time = c("14:27:22.130", 
"14:41:27.720", "14:41:27.940", "14:41:28.140", "14:41:28.170", 
"14:41:28.350", "14:41:28.390", "14:41:28.520", "14:41:28.630", 
"14:41:28.740"), id = c("999000000007628", "989001002807730", 
"989001002807730", "989001002807730", "989001002807730", "989001002807730", 
"989001002807730", "989001002807730", "989001002807730", "989001002807730"
), datetime = structure(list(sec = c(22.13, 27.72, 27.94, 28.14, 
28.17, 28.35, 28.39, 28.52, 28.63, 28.74), min = c(27L, 41L, 
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L), hour = c(14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L), mday = c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), mon = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), year = c(115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L), wday = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
     5L), yday = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), isdst = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), zone = c("PST", "PST", "PST", 
    "PST", "PST", "PST", "PST", "PST", "PST", "PST"), gmtoff =     c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", 
    "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
    "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), site = c("Chivato", 
    "Chivato", "Chivato", "Chivato", "Chivato", "Chivato", "Chivato", 
    "Chivato", "Chivato", "Chivato")), .Names = c("date", "time", 
    "id", "datetime", "site"), row.names = c(NA, 10L), class = "data.frame")

3 个答案:

答案 0 :(得分:3)

您可以使用uniqueN中的data.table功能:

library(data.table)
setDT(df)[, uniqueN(id), by = date]

或(根据@Richard Scriven的评论):

aggregate(id ~ date, df, function(x) length(unique(x)))

答案 1 :(得分:2)

或者我们可以使用n_distinct

中的library(dplyr)
library(dplyr) 
df %>%
   group_by(date) %>%
   summarise(id=n_distinct(id))

答案 2 :(得分:0)

这个回答是对这篇文章的回应:group by and then count unique observations在写这篇草稿时被标记为重复。这不是对此处重复基础的问题的回应:How to find number of unique ids corresponding to each date in a data drame询问有关查找唯一ID的问题。我不确定第二篇文章实际上是否回答了OP的问题,

  

“我想为每个人创建一个包含唯一id个数的表格   group1group2的组合。“

这里的关键字是'组合'。解释是每个id具有group1的特定值和group2的特定值,因此感兴趣的数据集是特定的值集c(id, group1, group2)。 / p>

以下是OP提供的data.frame:

df1 <- data.frame(id=sample(letters, 10000, replace = T),
group1=sample(1:2, 10000, replace = T),
group2=sample(100:101, 10000, replace = T))

使用受此帖子启发的data.table - https://stackoverflow.com/a/13017723/5220858

>library(data.table)
>DT <- data.table(df1)
>DT[, .N, by = .(group1, group2)]

   group1 group2    N
1:      1    100 2493
2:      1    101 2455
3:      2    100 2559
4:      2    101 2493

N是具有特定id值和特定group1值的group2的计数。展开以包含id也会返回包含104个唯一idgroup1group2组合的表格。

>DT[, .N, by = .(id, group1, group2)]

     id group1 group2   N
  1:  t      1    100 107
  2:  g      1    101  85
  3:  l      1    101  98
  4:  a      1    100  83
  5:  j      1    101  98
 ---                     
100:  p      1    101  96
101:  r      2    101  91
102:  y      1    101 104
103:  g      1    100  83
104:  r      2    100  77