1 Mathematical Calculations

Use R calculate the following

  1. 2x53x981x21
a <- 2
b <- 53
c <- 981
d <- 21
result <- a*b*c*d
print(result)
## [1] 2183706
2 * 53 * 981 * 21
## [1] 2183706
  1. (95x8734)/47
a <- 95
b <- 8734
resulta <- a * b
c <- 47
resultb <- resulta / 47
print(resultb)
## [1] 17653.83
(95*8734)/47
## [1] 17653.83
  1. \(2^{12}\)
a <- 2
b <- 12
resulta <- a ^ b
print(resulta)
## [1] 4096
2^12
## [1] 4096

2 Basic R manipulation.

  1. In the code segment below, assign the value 2 to variable x, and the value 6 to variable y. Then compute the sum ‘x+y’:
x = 2
y = 6
z = 2 + 8
print(z)
## [1] 10
  1. Construct a list of all even numbers between 1 and 100, assign this to the variable ‘even’. Construct a list of 100 numbers, starting with 1, such that the difference between consecutive numbers is three, assign this to the variable ‘threes’. Construct a list of 100 equally spaced numbers between 0 and 2. Assign this to the variable x.

list 1 -> 100 every 3s

a = 1
b = 100

threes = 3
seq(a, b, +3)
##  [1]   1   4   7  10  13  16  19  22  25  28  31  34  37  40  43  46  49  52  55
## [20]  58  61  64  67  70  73  76  79  82  85  88  91  94  97 100
  1. Define a function that takes input height and weight, and outputs the BMI for those values of height and weight.
print("enter weight in kg and hieght in meters")
## [1] "enter weight in kg and hieght in meters"
height = readline(prompt = "enter height: ")
## enter height:
weight = readline(prompt = "enter weight: ")
## enter weight:
hnum = as.numeric(height)
wnum = as.numeric(weight)
a = 2
b = 703
hnum2 = hnum^2
resulta = wnum / hnum2

print(resulta)
## [1] NA

Use this function to calculate your BMI.