计算具有约束的行的差异

时间:2017-01-15 20:21:23

标签: r dataframe

采用以下数据集:

df <- data.frame (Field = c(rep(c("F1","F2","F3"),each=3),rep(c("F4","F5"),each=2)),
              Plot  = c(rep(c("A","B","C"),3),rep(c("A","B"),2)),
              Value = runif(13, min = 0, max = 10),
              Variation = NA
              )

让我们说 Plot A 是控制图。我想计算图A 和其他(B和C)的变化。输出应显示在变体列中。

我的实际数据框架更加广泛和复杂,但我想这是一个好的开始。

1 个答案:

答案 0 :(得分:3)

终于找到了答案,我花了一段时间:

我现在将答案分成两部分,以显示原始答案和Rui的附加要求

1针对所有人的解决方案:

df <- data.frame (Field = c(rep(c("F1","F2","F3"),each=3),rep(c("F4","F5"),each=2)),
              Plot  = c(rep(c("A","B","C"),3),rep(c("A","B"),2)),
              Value = runif(13, min = 0, max = 10),
              Total = NA
)

然后使用此循环

Diff<- list()

for(i in 1:length(unique(df$Field))){
  temp <- dplyr::filter(df, Field == unique(Field)[i])
  Plots <- unique(temp$Plot)
  Combinations <- expand.grid(x = Plots, y = Plots)
  Combinations$Plots <- paste(Combinations$x, Combinations$y, sep ="-")
  Combinations$Field <- unique(df$Field)[i]

  Diff[[i]]<- Combinations
}

Diff <- do.call("rbind", Diff)

这将生成包含绘图和字段的所有组合的数据框,然后我们将使用dplyr::filter通过字段

获取x和y的值
Diff$Diff <- NA
for(j in 1:nrow(Diff)){
  Diff[j,]$Diff <- (dplyr::filter(df, Plot == Diff$x[j] & Field == Diff$Field[j])$Value - dplyr::filter(df, Plot == Diff$y[j] & Field == Diff$Field[j])$Value)
}

最后消除前两列(x和y),留下3列Plots,Field和Diff

Diff <- Diff[,-(1:2)]

希望它有所帮助,这是它的外观样本:

head(Diff)
   Plots Field       Diff
1   A-A    F1  0.00000000
2   B-A    F1 -1.25081916
3   C-A    F1 -1.20700858
4   A-B    F1  1.25081916
5   B-B    F1  0.00000000
6   C-B    F1  0.04381059

2首先针对所有人的解决方案:

Diff<- list()

for(i in 1:length(unique(df$Field))){
  temp <- dplyr::filter(df, Field == unique(Field)[i])
  Plots <- unique(temp$Plot)
  Combinations <- expand.grid(x = Plots[1], y = Plots)
  Combinations$Plots <- paste(Combinations$x, Combinations$y, sep ="-")
  Combinations$Field <- unique(df$Field)[i]

  Diff[[i]]<- Combinations
}

Diff <- do.call("rbind", Diff)

Diff$Diff <- NA
for(j in 1:nrow(Diff)){
  Diff[j,]$Diff <- (dplyr::filter(df, Plot == Diff$x[j] & Field == Diff$Field[j])$Value - dplyr::filter(df, Plot == Diff$y[j] & Field == Diff$Field[j])$Value)
}

Diff <- Diff[,-(1:2)]