If I have an R data frame that looks like this:
| Value | TestNum | RepNum |
|:-----:|:-------:|:------:|
| 104 | 1 | 1 |
| 101 | 1 | 2 |
| 101 | 1 | 3 |
| 100 | 2 | 1 |
| 100 | 2 | 2 |
| 100 | 2 | 3 |
| 90 | 3 | 1 |
| 90 | 3 | 2 |
| 90 | 3 | 3 |
| 91 | 4 | 1 |
| 94 | 4 | 2 |
| 94 | 4 | 3 |
| 105 | 5 | 1 |
| 105 | 5 | 2 |
| 108 | 5 | 3 |
Is there a way that I can modify this data frame to find the average for the 3 RepNum Values for each unique TestNum so that it looks like this:
| Mean | TestNum |
|:----:|:-------:|
| 102 | 1 |
| 100 | 2 |
| 90 | 3 |
| 93 | 4 |
| 106 | 5 |
You can create this example data frame in R by copying and pasting this code and executing it.
Value<-c(100,101,100,100,100,100,90,90,90,93,94,94,105,105,108)
TestNum<-c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5)
RepNum<-c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
df<-data.frame(Value,TestNum,RepNum)
EDIT: Here's a "more complete" example of the data frame I'm starting with an what I'm hoping to end up with:
| FileName | Version | Category | Value | TestNum | RepNum |
|:--------:|:-------:|:---------:|:-----:|:-------:|:------:|
| File1 | 1.0.1 | Category1 | 104 | 1 | 1 |
| File1 | 1.0.1 | Category1 | 101 | 1 | 2 |
| File1 | 1.0.1 | Category1 | 101 | 1 | 3 |
| File1 | 1.0.2 | Category1 | 100 | 2 | 1 |
| File1 | 1.0.2 | Category1 | 100 | 2 | 2 |
| File1 | 1.0.2 | Category1 | 100 | 2 | 3 |
| File1 | 1.0.4 | Category1 | 90 | 3 | 1 |
| File1 | 1.0.4 | Category1 | 90 | 3 | 2 |
| File1 | 1.0.4 | Category1 | 90 | 3 | 3 |
| File1 | 1.0.5 | Category1 | 94 | 4 | 1 |
| File1 | 1.0.5 | Category1 | 91 | 4 | 2 |
| File1 | 1.0.5 | Category1 | 94 | 4 | 3 |
| File1 | 1.0.8 | Category1 | 105 | 5 | 1 |
| File1 | 1.0.8 | Category1 | 105 | 5 | 2 |
| File1 | 1.0.8 | Category1 | 108 | 5 | 3 |
And ending with this:
| FileName | Version | Category | Mean_Value | TestNum |
|:--------:|:-------:|:---------:|:----------:|:-------:|
| File1 | 1.0.1 | Category1 | 102 | 1 |
| File1 | 1.0.2 | Category1 | 100 | 2 |
| File1 | 1.0.4 | Category1 | 90 | 3 |
| File1 | 1.0.5 | Category1 | 93 | 4 |
| File1 | 1.0.8 | Category1 | 106 | 5 |
As you've probably noticed, there is only 1 unique entry for both the FileName
column and the Category
column. The Version
column is changing along with the TestNum
column. So it seems like it might be easiest to simply add in these other columns after I've found the mean.
In the "full" code that I'm working on, I'm getting the mean values for several different files and many unique categories but I've been creating multiple data frames that are created by subsetting an original data frame on the FileName and Category (and an additional "Case" column).