subtraction in Diff R software

Asked

Viewed 436 times

1

my level in R is beginner in case you can help me with a problem I will be grateful.

I have a mathematical function that generates me 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 ... etc. However, since there is no value between the first column and the previous one, the value must be zero, I used the diff function to accomplish this, but I was not successful. Could you 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

code of the R

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)
  • Welcome to SOPT. Would like to [Edit] your post and add the code you are using, so we can analyze and suggest a change. Thank you.

  • add code to question friend.

  • Click [Edit] to enter the code

  • It is not clear what you want to do. Your final object, MCincrease is an array, so there are no columns. Do you want to subtract each value from the previous one? If it is, you should use diff even, the first value will be ignored if you want can include a 0 not to lose it: diff(c(0, MCiper)).

  • 1

    In addition, the code contains the if that you solved in the other question, it would be ideal to correct it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.