R 基于外围计数的子集数据帧

R 基于外围计数的子集数据帧,r,subset,R,Subset,我有一个如下所示的数据帧: df <- data.frame(Site=rep(paste0('site', 1:5), 50), Month=sample(1:12, 50, replace=T), Count=(sample(1:1000, 50, replace=T))) 如果将计数1分配给site5,则其计数始终为a数据。表解决方案: require(data.table) set.seed(45) df <- data.fram

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

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

如果将计数1分配给site5,则其计数始终为a
数据。表
解决方案:

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
require(data.table)
种子(45)

df这里有一个
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,]
###df2$如果计数>=max(计数)*0.05,则此月的测试为真
df2=(最大值(计数)*0.05))
##对于每个站点,如果本月至少有一个计数>=max(count)*0.05,则测试$keep为真
测试(0)
##子集

因此,删除count@RossAhmed所在站点的所有行,这应该可以做到。
df$Count[df$Site=='site2'] <- ceiling(seq(1, 1000, length.out=20))
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
## 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,]