Search code examples
rsubtraction

Difference calculation within a numeric vector in R


I have a vector

tyu <- c(11,8,5,4,2,1)

whose structure is

str(tyu)
num [1:6] 11 8 5 4 2 1

I would like to calculate the difference between the 1st value and the last value i.e the 6th value of the numeric vector using R programming

Answer would be = 11 - 1 = 10


Solution

  • We can use

    tyu[1] - tyu[length(tyu)]
    

    Or

    head(tyu,1) - tail(tyu, 1)