Posts

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