在subet之后维护数据框行

时间:2015-05-26 12:54:43

标签: r subset

我正在尝试根据子集计算某些数据的%收益率:

# example data set
set.seed(10)
Measurement <- rnorm(1000, 5, 2)
ID <- rep(c(1:100), each=10)
Batch <- rep(c(1:10), each=100)

df <- data.frame(Batch, ID, Measurement)

df$ID <- factor(df$ID)
df$Batch <- factor(df$Batch)

# Subset data based on measurement range

pass <- subset(df, Measurement > 6 & Measurement < 7)

# Calculate number of rows in data frame (by Batch then ID)

ac <- ddply(df, c("Batch", "ID"), nrow)
colnames(ac) <- c("Batch", "ID", "Total")

# Calculate number of rows in subset (by Batch then ID)

bc <- ddply(pass, c("Batch", "ID"), nrow)
colnames(bc) <- c("Batch", "ID", "Pass")

# Calculate yield 

bc$Yield <- (bc$Pass / ac$Total) * 100

# plot yield

ggplot(bc, aes(ID, Yield, colour=Batch)) + geom_point()

我的问题是,由于我的过滤范围(介于6和7之间),我的子集(pass)的行数少于我的数据帧(df)

nrow(ac)
[1] 100

nrow(bc)
[1] 83

因此我无法使用

    bc$Yield <- (bc$Pass / ac$Total) * 100

或者我收到错误

replacement has 100 rows, data has 83

我试图保持通用的原因是因为我的实际数据具有不同的批次和ID数量(否则我可以在我的收益率计算中除以常数)。如果数据超出限制(在这种情况下为6到7),任何人都可以告诉我如何在我的子集中放置0。或者指出一种更优雅的计算收益率的方法。谢谢

更新

str(df)

'data.frame':   1000 obs. of  3 variables:
 $ Batch      : Factor w/ 10 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ ID         : Factor w/ 100 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Measurement: num  5.04 4.63 2.26 3.8 5.59 ...

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的。我已经使用dplyr的group_by完成了它并在此汇总。

对于每个批次/ ID,它计算观察次数,测量值在6到7之间的观测数量以及这两者的比率。

library(dplyr)

# example data set
set.seed(10)
Measurement <- rnorm(1000, 5, 2)
ID <- rep(c(1:100), each=10)
Batch <- rep(c(1:10), each=100)

df <- data.frame(Batch, ID, Measurement)

df$ID <- factor(df$ID)
df$Batch <- factor(df$Batch)

# Subset data based on measurement range

countFunc <- function(x) sum((x > 6)&(x<7))

# Calculate number of rows, rows that meet criteria, and yield.

totals <- df %>% group_by(Batch, ID) %>%
  summarize(total = length(Measurement), x = countFunc(Measurement)) %>%
  mutate(yield = x/total) %>%
  as.data.frame()