# Create a 5x5 matrix
m <- matrix(c(11, 21, 22, 23, 13,
              14, 24, 25, 31, 32,
              42, 52, 33, 34, 35,
              41, 43, 44, 45, 51,
              12, 53, 54, 15, 55), nrow = 5, byrow = TRUE)

# 1. Sub-matrix of elements in rows 3, 4, or 5 and in columns 1 or 2
sub_matrix_1 <- m[3:5, 1:2]

# 2. Sub-matrix formed by deleting columns 2 and 4
sub_matrix_2 <- m[, -c(2, 4)]

# 3. Sub-matrix of rows whose first column element has an even ten's digit
even_tens_rows <- m[as.integer(substring(as.character(m[, 1]), 1, 1)) %% 2 == 0, ]

# 4. Sub-matrix of elements in column 5 that correspond to an element in the same row
# and the first column that is less than 50 (resulting in a column matrix)
sub_matrix_4 <- matrix(m[m[, 1] < 50, 5], ncol = 1)

# Display the results
print("Sub-matrix 1:")
print(sub_matrix_1)

print("Sub-matrix 2:")
print(sub_matrix_2)

print("Sub-matrix 3:")
print(even_tens_rows)

print("Sub-matrix 4:")
print(sub_matrix_4)
 

R Language Online Compiler

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.

About R

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.

Key Features

  • Interpreted programming language(no compilation required)
  • provides highly extensible graphical techniques.
  • Good community support
  • Free and open-source
  • Handles data very effectively.

Syntax help

Data Types

Data typeDescriptionUsage
NumericTo represent decimal valuesx=1.84
IntegerTo represent integer values, L tells to store the value as integerx=10L
ComplexTo represent complex valuesx = 10+2i
LogicalTo represent boolean values, true or falsex = TRUE
CharacterTo represent string valuesx <- "One compiler"
rawHolds raw bytes

Variables

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

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
    #code    
} 

If-else

if(conditional-expression){  
    #code if condition is true  
} else {  
    #code if condition is false  
} 

Nested-If-else

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  
}  

2. Switch:

Switch is used to execute one set of statement from multiple conditions.

switch(expression, case-1, case-2, case-3....)   

3. For:

For loop is used to iterate a set of statements based on a condition.

for (value in vector) {  
  # code  
} 

4. While:

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 
}  

5. Repeat:

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  
   }  
} 

Functions

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.

How to define a Function

func-name <- function(parameter_1, parameter_2, ...) {  
   #code for function body   
}  

How to call a Function

function_name (parameters)

Vectors

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.