##### Lesson 2: R is your companion ##### ##### Getting Started ##### # initialize or declare numbers a = 1 b = 10 # adding a and b a + b # multiplying a and b a*b # dividing a by b a/b exp(a) # 10 power b 10^b # logarithm of b with base e log(b) # logarithm of b with base 10 log10(b) # b power a b^a # round off 3.1415 to 0 significant digits round(3.1415,0) # round off 3.1415 to 1 significant digits round(3.1415,1) # round off 3.1415 to 2 significant digits round(3.1415,2) # round off 3.1415 to 3 significant digits round(3.1415,3) # square root of 16 sqrt(16) ## creating new variables ## # adding a and b and storing it in c1 c1 = a + b print(c1) # multiplying a and b and storing it in c2 c2 = a*b print(c2) # dividing a by b and storing it in c3 c3 = a/b print(c3) # exponential of a and store it in c4 c4 = exp(a) print(c4) # 10 power b ; store it in c5 c5 = 10^b print(c5) # logarithm of b with base e and store it in c6 c6 = log(b) print(c6) # combining numbers into a list x = c(2,3,10,-4,3.5) print(x) # create equally spaced sequence of numbers from 10 to 200 seq(from=10,to=200,by=10) # create equally spaced sequence of numbers from 0 to 10 by 2.5 seq(from=0,to=10,by=2.5) # create equally spaced sequence of numbers from 1 to 10 by 0.5 seq(from=1,to=10,by=0.5) # Getting help on the function round ?round # Get help on the command print ?print # get help on the command seq ?seq # get help on the command sqrt ?sqrt # Vectors x <- seq(from=1, to=10) y <- c(3,4,1,1,2,3.4,5,67.4,1,3.14) # vector operations # 20 times the vector x 20*x # add elements in vector x to the elements in vector y x+y # 100 times vector x + 50 times vector y 100*x+50*y # scalar operations # add 13 to the elements in x 13+x # add 1 to the elements in y 1+y # divide the elements in x by 2.2 x/2.2 # cube the elements in y y^3