BASICS
I. The R terminal
a. The R terminal is where the commands can be entered and the results
displayed
b. > R prompt means R is waiting for you to do something
i. 2+2
ii. 6*5
iii. # 3-2
c. Points to be noted –
i. Spaces don’t matter
ii. Standard arithmetic operators + , -, / are as expected
iii. All expressions followed by # are not evaluated as # is used to add a
comment in R programming
II. Assigning a value to a variable
a. Assignment operator in R is <- , - >and =
i. a <- 5
b. Remove rm() – To remove things from your workspace
III. Vectors
a. A vector is a sequence of data elements of the same basic type
b. Function –c – it concatenates values together.
c. sms < - c(0,1,2,0,0,0,1)
d. Function c is followed by parenthesis which contain the arguments to the
function.
e. All mathematical operations can be done on these vectors through R
f. sms +5
g. sms *5
IV. Other functions
a. sd()
b. median()
c. max()
d. min ()
e. length() – number of elements in the vector
Exercises
1. The sum of 100.1, 234.9, 12.01 = Ans :347.01
2. Type codes for assigning 10 to variable x, 20 to variable y
Calculate the product of x and y
Store the result in a new object called z
Inspect your workspace by typing ls(), and by clicking the Environment tab in
Rstudio
Find the three objects you created.
Make a vector of the objects x,y and z
Find the minimum, maximum, length, and variance of the vector
Remove the myvec object from your workspace.
3. The numbers below are the first ten days of rainfall amounts in centimeter. Read them into
a vector rainfall
0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3, 0.0, 0.1
Answer the following questions:
What was the mean rainfall, how about the standard deviation?
Calculate the cumulative rainfall (running total) over these ten days. Confirm that the
last value of the vector that this produces is equal to the total sum of the rainfall.
Which day saw the highest rainfall
4. Assume that we have registered the height and weight for four people: Heights in cm are
180, 165, 160, 193; weights in kg are 87, 58, 65, 100. Make two vectors height and weight with
the data. The body mass index (BMI) is defined as weight in kg/(height in m2). Make a vector
with the BMI values for the four people, and a vector with the natural logarithm to the BMI
values. Finally make a vector with the weights for those people who have a BMI larger than
25.