R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. R is a programming language developed by Ross Ihaka and Robert Gentleman at the University of Auckland in 1993
R can be downloaded for free from https://www.r-project.org/
R Studio allows the user to run R in a more user-friendly environment. It is open-source (i.e. free) and available at https://rstudio.com/products/rstudio/download/
25+3
## [1] 28
25-3
## [1] 22
25*3
## [1] 75
25/3 # division
## [1] 8.333333
25%/%3 # given the integer value when 25 is divided by 3
## [1] 8
2**3
## [1] 8
2^3
## [1] 8
Built-in functions refer to a set of pre-defined functions
exp(6)
## [1] 403.4288
sqrt(36) #square root of 36
## [1] 6
sum(2,3,4,5,6)
## [1] 20
log(64)
## [1] 4.158883
log(10,2) #log 10 to base 2
## [1] 3.321928
log(42,10)#log 42 to the base 10
## [1] 1.623249
log(5,3)
## [1] 1.464974
factorial(5) # 5!= 1*2*3*4*5
## [1] 120
abs(-8.5)#absolute value
## [1] 8.5
floor(3.8) #greatest integer less than 3.8
## [1] 3
ceiling(3.2) #next integer to 3.2
## [1] 4
rep(35,times=10) #repeate 35 10 times
## [1] 35 35 35 35 35 35 35 35 35 35
rep("Happy", times=5)
## [1] "Happy" "Happy" "Happy" "Happy" "Happy"
## [1] "Happy" "Happy" "Happy" "Happy" "Happy"
5:9 # display numbers from 5 to 9
## [1] 5 6 7 8 9
1:100
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100
seq(5,9) # generates a sequence of numbers from 5 to 9,
## [1] 5 6 7 8 9
seq(5,10,0.5) #generates a sequence of numbers starting from 5, incrementing by 0.5 till 10
## [1] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
seq(1,50,5)
## [1] 1 6 11 16 21 26 31 36 41 46
Relational operators in R are used to compare values and determine the relationship between them.
2<5
## [1] TRUE
3>9
## [1] FALSE
2+3==6 #to check whether 'is equal to'
## [1] FALSE
2!=3 #to check whether 'not equal to'
## [1] TRUE
we can assign a value to a variable using the assignment operator <- or the equal sign = syntax: variable_name <- value
x <- 48 # x is assigned the value 48
print(x)
## [1] 48
x/5
## [1] 9.6
x*2
## [1] 96
y<-"Happy"
rm(x) #to remove x from memory
rm(y)
In R, a vector is a fundamental data structure that can hold a collection of values of the same data type. Vectors are essential in R and are used extensively in data analysis, statistics, and programming. c() is used to create vectors.