查找数据框中所有网站的共同日期

时间:2013-03-18 15:35:24

标签: r

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

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"

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

3 个答案:

答案 0 :(得分:6)

它适用于splitintersectReduce的组合:

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

[1] "6th Nov"

答案 1 :(得分:2)

使用plyr的一种方式:

with(ddply(data, .(date), function(x) all(1:4 %in% x$site)), date[V1 == TRUE])
# [1] 6th Nov

答案 2 :(得分: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