# Tugas R Pro Ainiyya Nindika Saputri # NIM 22/498521/EK/24037 # R program to illustrate Vector # Vectors(ordered collection of same data type) X = c(2,4,6,8,10) # Change the number from (1,3,5,7,8) to (2,4,6,8,10) # Printing those elements in console print(X) # R program to illustrate a List # The first attributes is a numeric vector # containing the employee IDs which is # created using the 'c' command here empId = c(1, 2, 3, 4) # The second attribute is the employee name # which is created using this line of code here # which is the character vector empName = c("Ainiyya", "Nindika", "Putri", "Zilna") # Change the name from (Debi, Sandeep, Subham, Shiba) to (Ainiyya, Nindika, Putri, Zilna) # The third attribute is the number of employees # which is a single numeric variable. numberOfEmp = 2 # Change the number from 4 to 2 # We can combine all these three different # data types into a list # containing the details of employees # which can be done using a list command empList = list(empId, empName, numberOfEmp) print(empList) # R program to illustrate dataframe # A vector which is a character vector Name = c("Inayah", "Nur", "Faidah") # Change the name from (Amiya, Raj, Asish) to (Inayah, Nur, Faidah) # A vector which is a character vector Country = c("Indonesia", "Malaysia", "Singapura") # Change Language (R, Python, Java) to Country (Indonesia, Malaysia, Singapura) # A vector which is a numeric vector Age = c(16, 17, 18) # Change the number from (22,25,45) to (16,17,18) # To create dataframe use data.frame command # and then pass each of the vectors # we have created as arguments # to the function data.frame() df = data.frame(Name, Country, Age) print(df) # R program to illustrate a matrix A = matrix( # Taking sequence of elements c(2, 3, 4, 5, 6, 7, 8, 9, 10), # Change the number from (1,2,3,4,5,6,7,8,9) to (2,3,4,5,6,7,8,9,10) # No of rows and columns nrow = 3, ncol = 3, # By default matrices are # in column-wise order # So this parameter decides # how to arrange the matrix byrow = TRUE ) print(A) # R program to illustrate an array A = array( # Taking sequence of elements c(10, 11, 12, 13, 14, 15, 16, 17), # change the number from (1,2,3,4,5,6,7,8) to (10,11,12,13,14,15,16,17) # Creating two rectangular matrices # each with two rows and two columns dim = c(2, 2, 2) ) print(A) # R program to illustrate factors # Creating factor using factor() fac = factor(c("Car", "Motor", "Motor", "Motor", "Car", "Car", "Motor")) # Change ("Male","Female","Male","Male","Female","Male","Female") to ("Car", "Motor", "Motor","Motor", "Car", "Car", "Motor") print(fac) x <- 125 if(x > 100){ print(paste(x, "is greater than 100")) } # Change 100 to 125 and 10 to 100 x <- 6 # Check value is less than or greater than 10 if(x > 10){ print(paste(x, "is greater than 10")) }else{ print(paste(x, "is less than 10")) } # Change 5 to 6 x <- letters[2:8] for(i in x){ print(i) } # Change 4:10 to 2:8 # Defining matrix m <- matrix(3:10, 2) for (r in seq(nrow(m))) { for (c in seq(ncol(m))) { print(m[r, c]) } } # Change 2:15 to 3:10 x = 5 # Print 5 to 20 while(x <= 20){ print(x) x = x + 2 } # Change 1 to 5, 1 to 5 into 5 to 20, x+1 into x+2 x = 2 # Print 1 to 10 repeat{ print(x) x = x + 1 if(x > 10){ break } } # change x= 1 to x=2 and x> 5 to x>10 # Checks weather is either Sunny, Rainy or Windy func <- function(x){ if(x > 0){ return("Sunny") }else if(x < 0){ return("Rainy") }else{ return("windy") } } func(1) func(0) func(-1) # Change Positive, Negatif and Return into Sunny, Rainy adn Windy # Defining vector x <- 1:20 # Print even numbers for(i in x){ if(i%%2 != 0){ next #Jumps to next loop } print(i) } # Change 1:10 into 1:20 # R program to demonstrate the use of for loop # using for loop for (val in 2: 6) { # statement print(val) } #Change 1:5 to 2:6 # R program to illustrate # application of for loop # assigning strings to the vector Year <- c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') # using for loop to iterate # over each string in the vector for (Month in Year) { # displaying each string in the vector print(Month) } # Change Day in Week into Month in Year # R program to demonstrate the use of while loop val = 1 # using while loop while (val <= 7) { # statements print(val) val = val + 2 } # Change val <= 5 into 7 # Change Val + 1 into Val + 2 # R program to illustrate # application of while loop # assigning value to the variable # whose factorial will be calculated n <- 7 # Change n from 5 to 7 # assigning the factorial variable # and iteration variable to 1 factorial <- 1 i <- 1 # using while loop while (i <= n) { # multiplying the factorial variable # with the iteration variable factorial = factorial * i # incrementing the iteration variable i = i + 1 } # displaying the factorial print(factorial) # R program to demonstrate the use of repeat loop val = 3 # Change val from 1 into 3 # using repeat loop repeat { # statements print(val) val = val + 2 # Change val + 1 into val +2 # checking stop condition if(val > 10) { # Change Val> 5 into 10 # using break statement # to terminate the loop break } } # R program to illustrate # the application of repeat loop # initializing the iteration variable with 0 i <- 0 # using repeat loop repeat { # statement to be executed multiple times print("Lets Go") # Change Geeks 4 geeks into lets go # incrementing the iteration variable i = i + 1 # checking the stop condition if (i == 3) { # Change 1==5 into 1==3 # using break statement # to terminate the loop break } } # R program to illustrate # the use of break statement # using for loop # to iterate over a sequence for (val in 1: 10) { # checking condition if (val == 1) { # using break keyword break } # displaying items in the sequence print(val) } # Change val in 1: 5 into 1: 10 # R program to illustrate # the use of next statement # using for loop # to iterate over the sequence for (val in 1: 7) { # checking condition if (val == 2) { # using next keyword next } # displaying items in the sequence print(val) } # Change val in 1:5 into 1:17 # Chanage val === 3 i into vall === 2
Write, Run & Share R Language code online using OneCompiler's R Language online compiler for free. It's one of the robust, feature-rich online compilers for R language, running on the latest version 3.4. Getting started with the OneCompiler's R Language compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as R
and start coding.
R is very popular for data analytics which was created by Ross Ihaka and Robert Gentleman in 1993. Many big companies like Google, Facebook, Airbnb etc uses this language for data analytics. R is good for software developers, statisticians and data miners.
Data type | Description | Usage |
---|---|---|
Numeric | To represent decimal values | x=1.84 |
Integer | To represent integer values, L tells to store the value as integer | x=10L |
Complex | To represent complex values | x = 10+2i |
Logical | To represent boolean values, true or false | x = TRUE |
Character | To represent string values | x <- "One compiler" |
raw | Holds raw bytes |
Variables can be assigned using any of the leftward, rightward or equal to operator. You can print the variables using either print or cat functions.
var-name = value
var-name <- value
value -> var-name
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
#code
}
if(conditional-expression){
#code if condition is true
} else {
#code if condition is false
}
if(condition-expression1) {
#code if above condition is true
} elseif(condition-expression2){
#code if above condition is true
}
elseif(condition-expression3) {
#code if above condition is true
}
...
else {
#code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(expression, case-1, case-2, case-3....)
For loop is used to iterate a set of statements based on a condition.
for (value in vector) {
# code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
# code
}
Repeat is used tyo iterate a set of statements with out any condition. You can write a user-defined condition to exit from the loop using IF
.
repeat {
#code
if(condition-expression) {
break
}
}
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
func-name <- function(parameter_1, parameter_2, ...) {
#code for function body
}
function_name (parameters)
Vector is a basic data strucre where sequence of data values share same data type.
For example, the below statement assigns 1 to 10 values to x.
You can also use se() function to create vectors.
x <- 1:10
#using seq() function
x <- seq(1, 10, by=2)
the above statement prints the output as [1] 1 3 5 7 9
.