I need to replace the values of the two replica (A and B) in a data frame, with their mean.
This is the data frame:
Sample.Name <- c("sample01","sample01","sample02","sample02","sample03","sample03")
Rep <- c("A", "B", "A", "B", "A", "B")
Rep <- as.factor(Rep)
joy <- sample(1000:50000000, size=120, replace=TRUE)
values <- matrix(joy, nrow=6, ncol=20)
df.data <- cbind.data.frame(Sample.Name, Rep, values)
names(df.data)[-c(1:2)] <- paste("V", 1:20, sep="")
And this is the loop I tried to write to substitute the mean to the replica:
Sample <- as.factor(Sample.Name)
livelli <- levels(Sample)
for (i in (1:(length(livelli)))){
estrai.replica <- which(df.data == livelli[i])
media.replica <- apply(values[estrai.replica,], 2, mean)
foo <- rbind(media.replica)
}
The main problems are:
- in this way I have only the last row in my new data frame (foo), and
- I haven’t the name of the sample in any column.
Do you have any suggestion?
I think you want to
aggregateyour data frame. Try this: