Outsourced Data Subset

I have a dataframe that looks like this:

df <- data.frame(Site=rep(paste0('site', 1:5), 50),
           Month=sample(1:12, 50, replace=T),
           Count=(sample(1:1000, 50, replace=T)))

I want to delete any sites where the counter always amounts to <5% of the maximum monthly bill on all sites.

Maximum monthly count for all sites:

library(plyr)
ddply(df, .(Month), summarise, Max.Count=max(Count))

If account 1 is assigned the value 1, then its calculation always amounts to 5% of the maximum monthly bill in all sites. Therefore, I would like to delete the site5.

df$Count[df$Site=='site5'] <- 1

However, after assigning site2 to the new values, some of its calculations are <5% of the maximum monthly bill, while others → 5%. Therefore, I would not want to delete site2.

df$Count[df$Site=='site2'] <- ceiling(seq(1, 1000, length.out=20))

How can I multiply a dataframe to delete any sites where the count is always <5% of the maximum monthly bill? Let me know if the question is unclear and I will correct it.

+5
2

a plyr :

## df2$test is true if Count >= max(Count)*0.05 for this month
df2 <- ddply(df, .(Month), transform, test=Count>=(max(Count)*0.05))
## For each site, test$keep is true if at least one count is >= max(Count)*0.05 for this month
test <- ddply(df2, .(Site), summarise, keep=sum(test)>0)
## Subsetting
sites <- test$Site[test$keep]
df[df$Site %in% sites,]
+3

A data.table :

require(data.table)
set.seed(45)
df <- data.frame(Site=rep(paste0('site', 1:5), 50),
       Month=sample(1:12, 50, replace=T),
       Count=(sample(1:1000, 50, replace=T)))
df$Count[df$Site=='site5'] <- 1

dt <- data.table(df, key=c("Month", "Site"))
# set max.count per site+month
dt[, max.count := max(Count), by = list(Month)]
# get the site that is TRUE for all months it is present
d1 <- dt[, list(check = all(Count < .05 * max.count)), by = list(Month, Site)]
sites <- as.character(d1[, all(check == TRUE), by=Site][V1 == TRUE, Site])

dt.out <- dt[Site != sites][, max.count := NULL]
#       Site Month Count
#   1: site1     1   939
#   2: site1     1   939
#   3: site1     1   939
#   4: site1     1   939
#   5: site1     1   939
#  ---                  
# 196: site2    12   969
# 197: site2    12   684
# 198: site2    12   613
# 199: site2    12   969
# 200: site2    12   684
+6

All Articles