print("Hello, World!") #cat("\nWelcome to R Programming Language ") if (FALSE) { 'This is a demo for multi-line comments and it should be put inside either and single OR double quot' } myString <- "string in r programming" print(myString) ####################################################################### #Data Types in R #logical (true or false) l <- TRUE print(class(l)) #numeric n <- 23.7 print(class(n)) #integer i <- 2L print(class(i)) #complex number c <- 2+3i print(class(c)) #character ch <- "TRUE" print(class(ch)) #########################################################################33 #create a vector fruits <- c('apple', 'orange', 'grapes', 'mango') print(fruits) #check the data type use print(class(fruits)) #creating a list #c is a function which lets us combine elements into a vector list_r <- list(c(1, 2, 5), 12.4, 'sin') print(list_r) print(class(list_r)) #create a matrix M = matrix(c('s', 'a', 'e', 't', 'w', 'a'), nrow=3, ncol=2, byrow=TRUE) print(M) print(class(M)) #create an array arr <- array(c('green', 'red'), dim=c(3,3,2)) print(arr) ################################################################################## #factor in r #factor() x <- c("Banana", "Apple", "Pineapple", "Banana", "Apple") print(x) Flavors <- factor(x) #print(Flavors) print(class(Flavors)) ################################################################################## #create a DataFrame BMI <- data.frame( names= c('Matthew', 'Alice', 'Nazim'), gender= c('male', 'female', 'male'), height= c(176, 165.2, 172), age = c(19, 20, 34) ) print(BMI) ################################################################################### #variables #Assignment using equal operator var.1 = c(0,1,2,3) #Assignment using leftward operator var.2 = c('learn', 'R') #Assignment using forward operator c(TRUE, 1) -> var.3 #var.3 <- c(TRUE, 1) means the same thing as the line above print(var.1) cat('var.1 is ', var.1, '\n') cat('var.2 is ', var.2, '\n') cat('var.3 is ', var.3, '\n') #how to check variables #character var_x <- "Hello" cat("The class of var_x is ",class(var_x),"\n") #numeric var_x <- 34.5 cat("Now the class of var_x is ",class(var_x),"\n") #integer var_x <- 27L cat("Next the class of var_x becomes ",class(var_x),"\n") #Finding variables #print(ls()) print(ls(pattern="var")) print(ls(all.name=TRUE)) #Deleting variables rm(var.3) #print(var.3) rm(list=ls()) print(ls()) ###################################################################################### #Operators in R a = c(2, 3.3, 1.5) b = c(2, 4.5, 6) sum = a+b print(sum) sub = a-b cat('the subtraction of a and b is: ', sub, '\n') mul = a*b print(mul) div = a/b print(div) rem = a%%b print(rem) exp = a^b print(exp) #Relational Operators [<, >, ==, <=, >=] print(a<=b) #Colon Operators {display the range} v <- 2:10 print(v) ####################################################################################### #Decision Making in R --> Conditions using if-else x <- 20 if(is.integer(x)){ print('x is an integer') }else{ print('x is not an integer') } #Repeat loop v <- c('learn', 'loop') count <- 5 repeat{ print(v) count <- count + 1 if(count>10){ break } } #While loop print('While loop') v <- c("Hello", "while loop") count <- 2 while(count<7){ print(v) count <- count + 1 } #For loop v <- LETTERS[1:26] for(i in v){ print(i) } ################################################################################# #using BREAK statement to end loop x <- 1:5 for (val in x) { if (val == 3) { break } #END If print(val) } #END For cat("==============================================\n") #using NEXT statement to skip loop step x <- 1:5 for (val in x) { if (val == 3) { next } #END If print(val) } #END For ################################################################################### #Create a function to print squares in sequence new.function <- function(a){ for(i in 1:a){ b <- i^3 print(b) } } new.function(5) #example2 --> function new.function <- function(a, b, c){ result <- a*b*c print(result) } #call the function new.function(a=2, b=3, c=4) ############################################################################### #install packages in R #install.packages("XML") ############################################################################## #Data Reshaping #create vector objects city <- c("Sydney", "Melbourne", "Canberra", "Perth") state <- c("NSW", "VIC", "ACT", "WA") zipcode <- c(2000, 2600, 3000, 5000) #combine the three vectors into dataframe addresses <- cbind(city, state, zipcode) #print(addresses) #print a header cat("# # # # The First data frame\n") print(addresses) #create another dataframe new.address <- data.frame( city = c("Darwin", "Brisbane", "Adelaide", "Hobert"), state = c("NT", "QLD", "SA", "TAS"), zipcode = c(4000, 5000, 8000, 7000), stringsAsFactors =FALSE ) #print a header cat("# # # # The Second data frame\n") print(addresses) #Print the data frame print(new.address) #combine all rows in both dataframes all.addresses <- rbind(addresses, new.address) print(all.addresses)
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
.