I am running into some difficulty after trying to divide each element in a given row by that row's mean. A dummy set of data:
set.seed(1)
x <- cbind(Plant = letters[1:5],
as.data.frame(matrix(rnorm(60), ncol = 12)))
x
Therefore, for Plant a, I would like V1, V2...V12 to divided by the mean of that row.
I thought it could be done using:
x/rowMeans(x)
But I get the error:
Error in rowMeans(x) : 'x' must be numeric
I assume that this error is due to the format of the data, because it's a data.frame and not a vector. I however managed to calculate the mean per row, by changing the data's format:
library(data.table)
x.T <- as.data.table(x)
x.T[,list(Mean=rowMeans(.SD)), by=Plant]
From there, I am not sure where to go. I am thinking that a loop would work, but doing some searches, I see where it is not advised. I would therefore like to have the normalized data for each sample Plant. Any suggestions please?
The first error is coming from trying to take the mean including Plant
variable/column, which is non-numeric. Try:
cbind(x$Plant, x[,-1]/rowMeans(x[,-1]))