0

我正在处理一个大型数据集,我想找到所有站点共有的日期。

site <- c(1,1,1,2,2,2,2,3,3,4,4,4)
date <- c("4th Nov","5th Nov","6th Nov","5th Nov","6th Nov","7th Nov","8th Nov","5th Nov","6th Nov","6th Nov","7th Nov","8th Nov")
temperature <- c(3,5,7,3,6,8,5,3,5,7,8,9)
data <- data.frame(site,date,temperature)

common.dates(data)
[1] "6th Nov"

任何帮助,将不胜感激。谢谢!

4

3 回答 3

6

它与 、 和 的组合split一起intersect使用Reduce

Reduce(intersect, split(data$date, data$site))

[1] "6th Nov"
于 2013-03-18T16:07:16.837 回答
2

一种使用方式plyr

with(ddply(data, .(date), function(x) all(1:4 %in% x$site)), date[V1 == TRUE])
# [1] 6th Nov
于 2013-03-18T15:52:13.323 回答
1

你可以这样做(即使它没有优化):

dates <- union(NULL,data$date)
sites <- union(data$site,NULL)

w <- array(0,length(dates)) # number sites per date
for (i in sites)
    for (j in 1:length(dates))
        w[j] <- w[j] + dates[j] %in% data$date[data$site==i]

dates[w == length(sites)] # returns the dates common to all sites
于 2013-03-18T15:49:39.327 回答