I need to replace the values ββof two replicas (A and B) in a data frame with their average value.
This is a 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 was trying to write in order to replace the average with a 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) }
Main problems:
- So I only have the last line in my new data frame (foo) and
- I do not have a sample name in any column.
Do you have any suggestions?
source share