Posts

Showing posts from May, 2023

Programming in R - Dr Binu V P

About me Introduction R Data Types R Variables Syntax /Elements of R program comments/keywords R Input/Output R Operators Control Statements in R     if and if else statements      switch statement     for loop- break and next statement     while loop     repeat loop Functions           Functions in R          Recursive Functions in R   Data Structures            Data Structures in R          Strings         Vectors         Lists          Matrices and Arrays         Data Frames         Factors  R Graphs and Charts         R Plots          R Line Graphs         R Scatter Plots          R Bar Plots          R Pie Charts          R Histogram          R Box Plot          R Strip Charts         Saving Plots in R R Data Manipulation           R Data Sets           R Basic Statistics         Data Handling in R         Read and write CSV files in R          Read and write xlsx files in R R Statistics          Central Tendency- Mean Median Mode         Measures of Variability- Range Variance an

Introduction

R is a free and open-source scripting language developed by Ross Ihaka and Robert Gentleman also known as “R & R” of the Statistics Department of the University of Auckland.in 1993. It's an alternative implementation of the S programming language, which was widely used in the 1980s for statistical computing. The R environment is designed to perforrm complex statistical analysis and display results using many visual graphics. The R progamming languague is written in C, Fortran, and R itself. Most R packages are written in the R programming language, but heavy computational chucks are written in C, C++, and Fortran. R allows integration with Python, C, C++, .Net, and Fortran. R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementa

R Data Types

Data types are used in computer programming to specify the kind of data that can be stored in a variable. For effective memory consumption and precise computation, the right data type must be selected. Each data type has its own set of regulations and restrictions. There are fundamentally five data types in R. Though straight forward and obvious at first glance, they have a few surprises hidden in them. These elementary data types are: Numeric Integer Complex Character Logical These data types are often combined to form data structures. Let us explore the meaning of each data type in detail. Numeric Datatype in R Decimal values are called numerics in R. It is the default R data type for numbers in R. If you assign a decimal value to a variable x as follows, x will be of numeric type. Real numbers with a decimal point are represented using this data type in R. it uses a format for double-precision floating-point numbers to represent numerical values. # A simple R program # to illustrat

R Variables

A variable is a memory allocated for the storage of specific data and the name associated with the variable is used to work around this reserved block. The name given to a variable is known as its variable name. Usually a single variable stores only the data belonging to a certain data type. The name is so given to them because when the program executes there is subject to change hence it varies from time to time. Variables in R R Programming Language is a dynamically typed language, i.e. the R Language Variables are not declared with a data type rather they take the data type of the R-object assigned to them. This feature is also shown in languages like Python and PHP. Declaring and Initializing Variables in R Language R supports three ways of variable assignment: Using equal operator- operators use an arrow or an equal sign to assign values to variables. Using the leftward operator- data is copied from right to left. Using the rightward operator- data is copied from left to right. R

Syntax/elements of R program-comments, keywords

Image
A program in R is made up of three things: Variables, Comments, and Keywords. Variables are used to store the data, Comments are used to improve code readability, and Keywords are reserved words that hold a specific meaning to the compiler. Variables in R variables which like any other programming language are the name given to reserved memory locations that can store any type of data. In R, the assignment can be denoted in three ways: = (Simple Assignment) <- (Leftward Assignment) -> (Rightward Assignment) x=2 y<-3 4->z print(x) print(y) print(z) Comments in R Comments are generic English sentences, mostly written in a program to explain what it does or what a piece of code is supposed to do. More specifically, information that programmer should be concerned with and it has nothing to do with the logic of the code. They are completely ignored by the compiler and are thus never reflected on to the input. The question arises here that how will the compiler know whether the g

R Input/Output

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. Like other programming languages in R it’s also possible to take input from the user. For doing so, there are two methods in R. Using readline() method Using scan() method Using readline() method In R language readline() method takes input in string format. If one inputs an integer then it is inputted as a string, lets say, one wants to input 255, then it will input as “255”, like a string. So one needs to convert that inputted value to the format that he needs. In this case, string “255” is converted to integer 255. To convert the inputted value to the desired data type, there are some functions in R, as.integer(n); —> convert to integer as.numeric(n); —> convert to numeric type (float, double etc) as.complex(n); —> convert to complex number (i.e 3+2i) as.Date(n) —> conver

R Operators

Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R Operators R supports majorly four kinds of binary operators between a set of operands. In this article, we will see various types of operators in R Programming language and their usage. Types of the operator in R language Arithmetic Operators Logical Operators Relational Operators Assignment Operators Miscellaneous Operator Arithmetic Operators Arithmetic operations in R simulate various math operations, like addition, subtraction, multiplication, division, and modulo using the specified operator between operands, which may be either scalar values, complex numbers, or vectors. The R operators are performed element-wise at the corresponding positions of the vectors. Addition operator (+) The values at the corresponding

if and if-else Statements in R

Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements.These statements are used to make a decision after assessing the variable.  if condition This control structure checks the expression provided in parenthesis is true or not. If true, the execution of the statements in braces {} continues. Syntax: if(expression)           { statements .... ....            } Example: x <- 100 if(x > 10){ print(paste(x, "is greater than 10")) } output [1] "100 is greater than 10" if-else condition It is similar to if condition but when the test expression in if condition fails, then statements in else condition are executed. Syntax:                if(expression)                                    { statements .... .... }                  else{ statements .... .... } Example: x <- 5 # Check value is less than or greater than 10 if(x > 10) { print(paste(x, "is greater than 1

switch statement in R

Switch case statements are a substitute for long if statements that compare a variable to several integral values. Switch case in R is a multiway branch statement. It allows a variable to be tested for equality against a list of values. Switch statement follows the approach of mapping and searching over a list of values. If there is more than one match for a specific value, then the switch statement will return the first match found of the value matched with the expression. Syntax: switch(expression, case1, case2, case3....) Here, the expression is matched with the list of values and the corresponding value is returned. Important Points about Switch Case Statements: An expression type with character string always matched to the listed cases. An expression which is not a character string then this exp is coerced to integer. For multiple matches, the first match element will be used. No default argument case is available there in R switch case. An unnamed case can be used, if there is no

for loop in R - break and next statement

For loop in R Programming Language is useful to iterate over the elements of a list, data frame, vector, matrix, or any other object. It means, the for loop can be used to execute a group of statements repeatedly depending upon the number of elements in the object. It is an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false. for loop It is a type of loop or sequence of statements executed repeatedly until exit condition is reached. Syntax: for(value in vector) {    statements .... ....  } Example: for (i in seq(10)){   cat(paste(i," ")) } output: 1  2  3  4  5  6  7  8  9  10   for (i in 1:10){   cat(paste(i," ")) } output: 1  2  3  4  5  6  7  8  9  10   x <- letters[4:10] for(i in x) { print(i) } output [1] "d" [1] "e" [1] "f" [1] "g" [1] "h" [1] "i" [1] "j" # R Pro

while loop in R

While loop in R programming language is used when the exact number of iterations of a loop is not known beforehand. It executes the same code again and again until a stop condition is met. While loop checks for the condition to be true or false n+1 times rather than n times. This is because the while loop checks for the condition before entering the body of the loop. R- While loop Syntax:  while (test_expression) {   statement update_expression } How does a While loop execute?  Control falls into the while loop. The flow jumps to Condition Condition is tested. If the Condition yields true, the flow goes into the Body. If the Condition yields false, the flow goes outside the loop The statements inside the body of the loop get executed. Updation takes place. Control flows back to Step 2. The while loop has ended and the flow has gone outside. Important Points about while loop in R language: It seems to be that while the loop will run forever but it is not true, condition is provided to

repeat loop in R

Repeat loop in R is used to iterate over a block of code multiple number of times. And also it executes the same code again and again until a break statement is found. Repeat loop, unlike other loops, doesn’t use a condition to exit the loop instead it looks for a break statement that executes if a condition within the loop body results to be true. An infinite loop in R can be created very easily with the help of the Repeat loop. The keyword used for the repeat loop is 'repeat'. Syntax : repeat {  commands   if(condition) {   break }  } # R program to illustrate repeat loop result <- 1 # test expression repeat { print(result) # update expression result = result + 1 # Breaking condition if(result > 5) { break } } output: [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 #checking for prime n <- 21 b <- a/2 flag <- 0 i <- 2 repeat { if ((n %% i)== 0) { flag <- 1 break } if (i>b) { break}     i=i+1 } if (flag == 1) { print("composite") }else{ p

Functions in R

Functions are useful when you want to perform a certain task multiple times. A function accepts input arguments and produces the output by executing valid R commands that are inside the function. In R Programming Language when you are creating a function the function name and the file in which you are creating the function need not be the same and you can have one or more functions in R. Creating a Function in R Functions are created in R by using the command function(). The general structure of the function file is as follows: Syntax: function_name = function(arg_1, arg_2, …) { Function body }   The various components/parts of a function are: Function name: It is the actual name of the function. It is stored in R environment as an object with this name. Arguments: An argument is a placeholder. Whenever a function is invoked, a value if passed to the argument.They are optional; that is, a function may contain no arguments. Also arguments can have default values. Function Body: It co