0% found this document useful (0 votes)
9 views7 pages

R Programming Basics: Data Types & Operators

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

R Programming Basics: Data Types & Operators

r-programming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment -2: Learn all the basics of R-programing (Data types, Variables, Operators et.

,)

What is Data type:

 A data type in R tells what kind of value a variable holds, such as a number, text, or
true/false.

 Data is categorized into

1. Numeric

2. Integer

3. Character

4. Logical

5. Complex

1. Numeric

 Used for real numbers with or without decimals.

Example:

x <- 25.6 print(x)

print(class(x))

Output: [1] 25.6

[1] "numeric"

2. Integer

 Used for whole numbers. Use L to define integer explicitly.

Example:

x <- 25L

print(x)

print(class(x))

Output: [1] 25
[1] "integer"

3. Character

 Used for storing text or strings.

Example:

x <- "R Programming"

print(x)

print(class(x))

Output:[1] "R Programming“

[1] "character"

4. Logical

 Used for boolean values — TRUE or FALSE.

Example:

x <- TRUE

y <- FALSE

print(x)

print(class(x))

Output:[1] TRUE

[1] "logical"

[Link]

 Used to store complex numbers (with imaginary part).


Example:

x <- 2 + 3i

print(x)

print(class(x))

Output: [1] 2+3i

[1] "complex"

Operators In R:

In R programming, operators are symbols or special characters used to perform operations on


variables and values. R supports several types of operators:

1. Arithmetic Operators

Used for basic mathematical operations.

Operator Description Example Result


+ Addition 3 + 2 5
- Subtraction 5 - 2 3
* Multiplication 4 * 3 12
/ Division 10 / 2 5
^ or ** Exponentiation 2 ^ 3 8
%% Modulus (remainder) 10 %%3 1
%/% Integer Division 10 %/%3 3
2. Relational Operators : Used to compare values and return TRUE or FALSE.

Operator Description Example Result


== Equal to 3 == 3 TRUE
!= Not equal to 4 != 5 TRUE
> Greater than 5 > 3 TRUE
< Less than 2 < 5 TRUE
>= Greater than or equal to 5 >= 5 TRUE
<= Less than or equal to 3 <= 4 TRUE

3. Logical Operators : Used to perform logical operations.

Operator Description Example Result


TRUE &
& Element-wise AND FALSE
FALSE

| | Element-wise OR TRUE
! NOT !TRUE FALSE
TRUE &&
&& First element AND FALSE
FALSE
First element OR
` `
4. Assignment Operators :

Used to assign values to variables.

Operator Description Example


<- Leftward assignment x <- 10
-> Rightward assignment 10 -> x
= Assignment (alternative to <-) x = 10
5. Miscellaneous Operators:

a) Colon operator : – creates a sequence of numbers.

Example: 1:5

#12345

b)( %in% )operator – checks if element belongs to a vector.

Example:

x <- c(1,2,3)

Print(2 %in% x)

Output: TRUE

c) Matrix multiplication %*%

Example:

A <- matrix(1:4, nrow=2)

B <- matrix(5:8, nrow=2)

A %*% B

Output: [,1] [,2]


[1,] 23 31
[2,] 34 46
3. Write R command to

i. illustrate summation, subtraction, multiplication, and division operations on vectors using


vectors

a <- c(10, 20, 30, 40, 50)

b <- c(1, 3, 5, 7, 9)

result <- a + b

print(result)

result1 <- a - b

print(result1)
result2 <- a * b
print(result2)
result3 <- a / b
print(result3)
Output:
ii. Enumerate multiplication and division operations
between matrices and vectors in R console
vector1=c(1,2,3,4,5,6,7,8,9,10,11,12) # create a vector for matrix elements

matrix1 <- matrix(vector1, nrow=2,ncol=6) # Create A matrix with 2 rows and 6 columns
mul_vec=c(1,2,3,4) # multiplication vector

print(matrix1*mul_vec) # print multiplication result

print(matrix1%%mul_vec)

Output:

Common questions

Powered by AI

Assignment operators in R include '<-' for leftward assignment, '->' for rightward assignment, and '=' as an alternative method of assignment. These operators allow programmers to store values in variables, enabling further manipulation and use within the program .

Understanding different data types in R is crucial because it determines how data is stored, manipulated, and how operations are applied. For instance, Numeric and Integer types handle numerical calculations, while Character types allow text processing. Logical types are useful for control flow and condition checks. Appropriate usage ensures efficient memory usage and precise data manipulation .

Logical operators in R perform logical operations between Boolean values. They include '&' for element-wise AND, '|' for element-wise OR, and '!' for NOT operation. Unlike relational operators that compare values directly, logical operators are used to manipulate and combine Boolean expressions, often as part of conditional statements or loops .

Arithmetic operators in R are used for basic mathematical computations. Examples include: '+' for addition (e.g., 3 + 2 results in 5), '-' for subtraction (e.g., 5 - 2 results in 3), '*' for multiplication (e.g., 4 * 3 is 12), '/' for division (e.g., 10 / 2 results in 5), '^' or '**' for exponentiation (e.g., 2 ^ 3 gives 8), '%%' for modulus (remainder) (e.g., 10 %% 3 is 1), and '%/%' for integer division (e.g., 10 %/% 3 results in 3).

The primary data types in R are Numeric, Integer, Character, Logical, and Complex. Numeric is used for real numbers, either with or without decimals, such as 'x <- 25.6'. Integer is used for whole numbers and explicitly defined using the 'L' suffix, like 'x <- 25L'. Character is for storing text or strings. Logical is for Boolean values, i.e., TRUE or FALSE, and Complex is for numbers with an imaginary part, demonstrated by 'x <- 2 + 3i' .

In R, matrix multiplication is performed using the %*% operator. For example, given matrices A and B, the expression 'A %*% B' computes the product of the two matrices following the rules of linear algebra, where the rows of the first matrix are multiplied element-wise with the columns of the second matrix and summed. This operation is fundamental in linear transformation applications .

Vectorized operations, such as performing '+' and '*' between vectors a <- c(10, 20, 30, 40, 50) and b <- c(1, 3, 5, 7, 9), enhance computational efficiency by applying operations on entire vectors at once rather than iterating through each element individually. R is optimized for vectorized operations, making calculations faster and more succinct, which is crucial for large datasets and complex computations .

The colon operator ':' is used in R to create a sequence of numbers, for example, '1:5' generates the sequence 1, 2, 3, 4, 5. On the other hand, the ' %in% ' operator checks membership, determining if an element is part of a vector, like '2 %in% c(1,2,3)', which returns TRUE. These operators serve different purposes: sequence creation and membership testing, respectively .

Relational operators in R are used to compare values and yield TRUE or FALSE. They include '==' for checking equality, '!=' for inequality, '>' and '<' for greater and lesser comparisons, respectively, and '>=' and '<=' to check greater than or equal to and less than or equal to comparisons. These operators are essential for conditional operations and control flow in programming .

Vector and matrix operations in R provide significant performance advantages over traditional loop-based approaches due to their vectorized nature. Operations are applied directly to entire data structures, allowing for more concise, readable, and less error-prone code. Furthermore, internal optimizations in R make vectorized calculations significantly faster, especially with large-scale computations, enhancing both efficiency and code simplicity .

You might also like