My level in R is beginner if you can help me with a problem I'll be grateful.
I have a math function that generates a vector, and I have to subtract between the value of the second column minus the value of the first column, between the value of the third column and the value of the second column ... etc. But since it does not have a value between the first column and the previous one the value should be zero, I used the diff function to accomplish this, but I did not succeed. Would you like to help me? Examples:
Example below.
0.5863 0
0.5908 0.0045; 0, 5908-0.5863 = 0.0045
0.5954 0.0046; 0.5954 - 0.5908 = 0.0046
R code
BWGmax <- 32
K <- 0.0118
Xm <- 26
SD <- 1.00850750718386
mean.b<- 30
DLmeth <- 0.0273
CDiet <- 0.329
CDLMET <- 1
WB <- 1
number <- 4300
BWG.EN <- 1.3
AMEn <- 2.975
Temp <-31
mt <- (300.14 - 14.61 * Temp + 0.2876 * Temp**2)
MCic <- seq(from = 210, to = 290, by = 1.9)
MCisimulate <- rep(MCic, number)
BW <- 0.0223*WG^0.8944
WG <- c()
for(i in 1:number) {
WG[i] <- ((sqrt(-2*log(runif(1, 0, 1)))*sin(2*pi*runif(1, 0, 1)))*SD)+(mean.b)
}
t <- matrix(BW, nrow = 43, ncol = 100)
BW2 <- apply(t, 1, mean)
BWG2 <- if ((BWGmax*(1-exp(-K*(MCisimulate-(Xm-BW))))) > WG) { WG } else { (BWGmax*(1-exp(-K*(MCisimulate-(Xm-BW))))) } mean(BWG2)
k <- matrix(BWG2, nrow = 43, ncol = 100)
BWG3 <- apply(k, 1, mean)
FI <- (mt*(BW^0.75)+(BWG2*BWG.EN))/AMEn
h <- matrix(FI, nrow = 43, ncol = 100)
FI2 <- apply(h, 1, mean)
MCiper <- ((MCic/1000)/FI2)*100
MCincrease <- diff(MCiper)