0% found this document useful (0 votes)
7 views11 pages

2 Program

The document provides a comprehensive introduction to R programming, covering essential concepts such as variables, data types, and operators. It explains how to create and manipulate various R objects, including vectors, lists, matrices, arrays, and factors, along with their respective functions. Additionally, it details different types of operators in R, including arithmetic, assignment, comparison, and logical operators.

Uploaded by

avunuridevaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

2 Program

The document provides a comprehensive introduction to R programming, covering essential concepts such as variables, data types, and operators. It explains how to create and manipulate various R objects, including vectors, lists, matrices, arrays, and factors, along with their respective functions. Additionally, it details different types of operators in R, including arithmetic, assignment, comparison, and logical operators.

Uploaded by

avunuridevaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Learn all the basics of R-Programming (Data types, Variables, Operators etc.

A) Variables:
Variables are nothing but reserved memory locations to store values. This means that, when
create a variable you reserve some space in memory. A variable provides us with named
storage that our programs can manipulate. A variable in R can store an atomic vector, group
of atomic vectors or a combination of many Robjects. A valid variable name consists of
letters, numbers and the dot or underline characters. The variable name starts with a letter or
the dot not followed by a number.
name<-"John"
age<-40

name#output"John"
age # output 40
The variables can be assigned values using leftward, rightward and equal to operator. The
values of the variables can be printed using print()
# Assignment using equal operator.
var.1=c(0,1,2,3)
# Assignment using leftward operator.
var.2<- c("learn","R")
# Assignment using rightward operator.
c(TRUE,1)->var.3
print(var.1)
print(var.2)
print(var.3)

B) Data Types
 In programming, data type is an important concept.
 Variables can store data of different types, and different types can do different things.
 In R, variables do not need to be declared with any particular type, and can even change
type after they have been set:

Example
my_var<-30#my_varistypeofnumeric
my_var <- "Sally" # my_var is now of type character
R has a variety of data types and object classes.
Basic Data Types

Basic data types in R can be divided into the following types:

 numeric - (10.5, 55, 787)


 integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
 complex - (9 + 3i, where "i" is the imaginary part)
 character (string) - ("k", "R is exciting", "FALSE", "11.5")
 logical (boolean) - (TRUE or FALSE)

We can use the class() function to check the data type of a variable:

#numeric
x<-10.5
class(x)

#integer
x<-1000L
class(x)

#complex
x<-9i+3
class(x)

#character/string
x<-"Risexciting"
class(x)

#logical/boolean
x<-TRUE
class(x)

R-objects.

 Vectors

 Lists

 Matrices

 Arrays
 Factors

Vectors

When you want to create vector with more than one element, you should use c() function
which means to combine the elements into a vector.

# Create a vector.

apple <- c('red','green',"yellow")

print(apple)

o/p: "red" "green" "yellow"

# Get the class of the vector.

print(class(apple))

o/p: "character"
Lists

A list in R can contain many different data types inside it. A list is a collection of data which
is ordered and changeable.

To create a list, use the list() function:

Example
#Listofstrings
thislist<-list("apple","banana","cherry")

#Printthelist
thislist
Access Lists

You can access the list items by referring to its index number, inside brackets. The first item
has index 1, the second item has index 2, and so on:
Example
thislist<-list("apple","banana","cherry")

thislist[1]
Change Item Value

To change the value of a specific item, refer to the index number:

Example
thislist<-list("apple","banana","cherry")
thislist[1]<-"blackcurrant"

#Printtheupdatedlist
thislist
List Length

To find out how many items a list has, use the length() function:

Example
thislist<-list("apple","banana","cherry")

length(thislist)
Add List Items

To add an item to the end of the list, use the append() function:

Example

Add "orange" to the list:

thislist<-list("apple","banana","cherry")

append(thislist, "orange")
Remove List Items

You can also remove list items. The following example creates a new, updated list without an
"apple" item:

Example

Remove "apple" from the list:


thislist<-list("apple","banana","cherry")

newlist<-thislist[-1]

#Printthenewlist
newlist
Range of Indexes

You can specify a range of indexes by specifying where to start and where to end the range,
by using the : operator:

Example

Return the second, third, fourth and fifth item:

thislist <- list("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")

(thislist)[2:5]
Matrices

A matrix is a two dimensional data set with columns and rows.

A column is a vertical representation of data, while a row is a horizontal representation of


data.

A matrix can be created with the matrix() function. Specify the nrow and ncol parameters to
get the amount of rows and columns:

Example
#Createamatrix
thismatrix<-matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)

#Printthematrix
thismatrix
2) # Create a matrix.
M =matrix( c('a','a','b','c','b','a'),nrow=2,ncol=3,byrow= TRUE)
print(M)
o/p: [,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"
Arrays
While matrices are confined to two dimensions, arrays can be of any number of dimensions.
The array function takes a dim attribute which creates the required number of dimension. In
the below example we create an array with two elements which are 3x3 matrices each.
# Create an array.
a <- array(c('green','yellow'),dim= c(3,3,2))
print(a)

o/p:
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"

Factors

Factors are the R-objects which are created using a vector.

It stores the vector along with the distinct values of the elements in the vector as labels. The
labels are always character irrespective of whether it is numeric or character or Boolean etc.
in the input vector. They are useful in statistical modeling. Factors are created using the
factor() function. The nlevels functions gives the count of levels.

# Create a vector.

apple_colors<- c('green','green','yellow','red','red','red','green')

# Create a factor object.

factor_apple<- factor(apple_colors)

# Print the factor.

print(factor_apple)

print(nlevels(factor_apple))

C) Operators:
Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example
10 + 5

R divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Miscellaneous operators

R Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical
operations:

Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

^ Exponent x^y
%% Modulus (Remainder from x %% y
division)

%/% Integer Division x%/%y

R Assignment Operators

Assignment operators are used to assign values to variables:

Example
my_var<-3

my_var<<-3

3->my_var

3->>my_var

my_var # print my_var

It is also possible to turn the direction of the assignment operator.

x <- 3 is equal to 3 -> x


R Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


R Logical Operators

Logical operators are used to combine conditional statements:

Operator Description

& Element-wise Logical AND operator. It returns TRUE if both elements are
TRUE

&& Logical AND operator - Returns TRUE if both statements are TRUE

| Elementwise- Logical OR operator. It returns TRUE if one of the statement is


TRUE

|| Logical OR operator. It returns TRUE if one of the statement is TRUE.

! Logical NOT - returns FALSE if statement is TRUE

Example:
Arithmetic Operators
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
Relational Operators
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
Logical Operators
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
Assignment Operators
v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 = c(3,1,TRUE,2+3i)
print(v1)
print(v2)
print(v3)

You might also like