The fight against the creation of a difference function

So, I have a home problem that I am really trying to code in R.

This is the problem: Write a function difference()that takes the vector X as a parameter and returns the vector difference between each element and the following element: X[2]-X[1], X[3]-X[2], X[4]-X[3]etc.

So difference(c(5,2,9,4,8))will returnc(-3,7,-5,4)

And so far I have this:

difference<-function(X) {
  for (i in X)
   X.val<-X[i]-X[i-1]
   return(X.val)
    }
difference(c(5,2,9,4,8))

It seems that I do not want the function to subtract X[2]-X[1], and when the function starts, it returns another number that is needed. Can anybody help me?

+4
source share
1 answer

. , , , , . , , - , . , , .

, for (i in X), X, . , i 5, 2, 9, 4, 8. == 5, : X.val <- X[5] - X[5 - 1]. X.val 4, X[5] 8, X[4] 4. == 2. , X.val -3 X[2] 2 X[1] 5.

, X. , for (i in 1:length(X)), length(X) , X.

, , , . , , , i. : 1?

, X.val . , , , NA, , 8, X 8 . , X.val, .

, .

# 1

, :

difference <- function(X) {
  for (i in 2:length(X)) {
    X[i] <- X[i] - X[i-1]
  }
  return(X)
}
difference(c(5, 2, 9, 4, 8))

, . .

, X, . c(5,2,9,4,8), X, X[i] <- X[i] - X[i-1] . , , :

1:
  • i 2
  • X[2] 2
  • X[i] <- X[i] - X[i-1], : X[2] <- X[2] - X[1]X[2] <- 2 - 5X[2] <- -3
  • X[2] -3
2:
  • i 3
  • X[3] 9
  • X[i] <- X[i] - X[i-1], : X[3] <- X[3] - X[2]X[3] <- 9 - -3X[3] <- 12
  • X[3] 12

, X, , .

, X.val, . , . :

difference <- function(X) {
  for (i in 2:length(X)) {
    X.val[i] <- X[i] - X[i-1]
  }
  return(X.val)
}

, , X . , . , , , x.diff . , , , . R, , , . , - class, . , , , X.val a numeric. :

difference <- function(X) {
  X.val <- numeric()
  for (i in 2:length(X)) {
    X.val[i] <- X[i] - X[i-1]
  }
  return(X.val)
}

, X.val for. , , for , .

, . , . , - NA. , ? : i.

# 2

, , , , R. R , . , :

a <- 1:10
b <- 11:20
a + b
a - b
a * b
a / b

, R "" . , a - b , . , a b - , . , , ? : .

x <- c(5, 2, 9, 4, 8)
y <- x[2:length(x)]
z <- x[1:(length(x)-1)]
y - z

, y - z , . difference :

difference <- function(X) {
  y <- X[2:length(X)]
  z <- X[1:(length(X)-1)]
  return(y-z)
}

, for, R, , R. , y z , :

difference <- function(X) {
  return(X[2:length(X)] - X[1:(length(X)-1)])
}

, , . , . R , : head() tail(). head , tail . .

a <- 1:50
head(a) # defaults to 6 elements
tail(a) # defaults to 6 elements
head(a, n=20) # we can change how many elements to return
tail(a, n=20)
head(a, n=-1) # returns all but the last element
tail(a, n=-1) # returns all but the first element

, . difference X[2:length(X)], " X, ". X[1:(length(X)-1)], " X ". :

difference <- function(X) {
  return(tail(X, -1) - head(X, -1))
}

, .

, . . -, return , . R , , . , :

difference_1 <- function(X) {
  x.diff <- tail(X, -1) - head(X, -1)
}
difference_1(1:10)

difference_2 <- function(X) {
  tail(X, -1) - head(X, -1)
}
difference_2(1:10)

difference_1 , . , . return.

- , - , . difference, (, , -, ), X.val, "" , , , :

x.val <- numeric()
length(x)

x.val[1] <- 1
length(x)

x.val[2] <- 2
length(x)

, . R-. - X.val , , . , . :

difference <- function(X) {
  x.val <- numeric(length=(length(X) - 1))
  for (i in 2:length(X)) {
    x.val[i-1] <- X[i] - X[i-1]
  }
  return(x.val)
}

. , .

, R. !

+6

All Articles