腳本計算範圍,然後計算每個目標樣本的數量內,或者你嘗試過什麼定義的範圍
# data from colleagues
df <- data.frame(target=c("RSV", "RSV", "RSV", "RSV",
"HSV", "HSV", "HSV",
"SRV", "SRV", "SRV"),
temperature=c(87.2, 86.9, 86.8, 86.7,
84.3, 89.7, 88.7,
54.3, 59.7, 58.7))
# target with ranges
res <- data.frame(target=character(0),
min.temperature=numeric(0),
max.temperature=numeric(0),
within=numeric(0),
outside=numeric(0))
# targets
l <- levels(df$target)
for(i in 1:length(l)) {
t <- df[df$target==l[i],]$temperature
# some way of defining these ranges
t.min <- min(t)
t.max <- max(t)
# targets in [min; max]
in.range <- df$temperature >= t.min &
df$temperature <= t.max
t.within <- nrow(df[df$target==l[i] & in.range,])
t.outside <- nrow(df[df$target==l[i] & !in.range,])
res <- rbind(res, data.frame(target=l[i],
min.temperature=t.min,
max.temperature=t.max,
within=t.within,
outside=t.outside))
}
print(res)
# target min.temperature max.temperature within outside
# 1 HSV 84.3 89.7 3 0
# 2 RSV 86.7 87.2 4 0
# 3 SRV 54.3 59.7 3 0
之外。你想要的輸出是什麼。請提出這個問題[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – r2evans
請參閱'?cut'或'?findInterval'來定義您的範圍值。 – thelatemail