y <- c(x,0.55, x, x)y[1] 10.0 5.0 3.0 6.0 0.55 10.0 5.0 3.0 6.0[10] 10.0 5.0 3.0 6.028

CHAPTER 2.DATA OBJECTS2.2.DATA STRUCTURESTyping the name of an object in the commands window results in printing the object.The numbers between square brackets indicate the position of the following element inthe vector.Use the functionroundto round the numbers in a vector.round (y,3)# round to 3 decimalsMathematical calculations on vectorsCalculations on (numerical) vectors are usually performed on each element. For example,x*xresults in a vector which contains the squared elements ofx.x[1] 10 5 3 6z <- x*xz[1] 100 25 9 36The symbols for elementary arithmetic operations are+,-,*,/. Use the^symbol toraise power.Most of the standard mathematical functions are available in R. Thesefunctions also work on each element of a vector. For example the logarithm of x:log(x)[1] 2.302585 1.609438 1.098612 1.791759Function nameOperationabsabsolute valueasin acos ataninverse geometric functionsasinh acosh atanhinverse hyperbolic functionsexp logexponent and natural logarithmfloor ceiling trunccreates integers from floating point numbersgamma lgammagamma and log gamma functionlog10logarithm with basis 10roundroundingsin cos tangeometric functionssinh cosh tanhhyperbolic functionssqrtsquare rootTable 2.1: Some mathematical functions that can be applied on vectors29

CHAPTER 2.DATA OBJECTS2.2.DATA STRUCTURESThe recycling ruleIt is not necessary to have vectors of the same length in an expression. If two vectorsin an expression are not of the same length then the shorter one will be repeated untilit has the same length as the longer one. A simple example is a vector and a number(which is a vector of length one).sqrt(x) + 2[1] 5.162278 4.236068 3.732051 4.449490In the above example the 2 is repeated 4 times until it has the same length asxand thenthe addition of the two vectors is carried out. In the next example,xhas to be repeated1.5 times in order to have the same length asy. This means the first two elements ofxare added toxand thenx*yis calculated.x <- c(1,2,3,4)y <- c(1,2,3,4,5,6)z <- x*yWarning message:longer object lengthis not a multiple of shorter object length in: x * y> z[1]149 165 12Generating vectorsRegular sequences of numbers can be very handy for all sorts of reasons. Such sequencescan be generated in different ways. The easiest way is to use the column operator (:).index <- 1:20index[1]123456789 10 11 12 13 14 15 16 17 18 19 20A descending sequence is obtained by20:1. The function seq together with its argumentsfrom, to, by or length is used to generate more general sequences. Specify the beginningand end of the sequence and either specify the length of the sequence or the increment.u <- seq(from=-3,to=3,by =0.5)u[1] -3.0 -2.5 -2.0 -1.5 -1.0 -0.50.00.51.01.52.02.53.030

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 212 pages?

Upload your study docs or become a

Course Hero member to access this document

Term

Fall

Professor

Staff

Tags