P2 - Basics of R Programming
P2 - Basics of R Programming
HELP COMMAND IN R
Help command
describe the in- >help(keyword_name)
built keyword. >?keyword_name
There are three >??keyword_name
ways to get >help.start() #whole help
description document/tutorial
about a inbuilt- >apropos(‘some part of keyword
keyword, name’) #list out the matching
package or keywords
function in R.
COMMENTS
• Single Line Comment
> # This Line is a Comment
> a = 5 # After assignment in variable a, rest line is a comment
>
NAMES
• Symbols Allowed in a name: {. and _}
• All numerical values {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} are allowed.
• All characters {a-z and A-Z} are allowed.
• Only . and characters are allowed to start a name and rest can be used anywhere.
• R is a case sensitive language.
• Some names are restricted in R (they are called reserved keywords).
TRUE
else FALSE
NaN
if NULL
NA_real_ Inf
functio
break KEYWORDS n
NA_integer NA_character_
next for
NA_complex_
NA while
repeat
OPERATORS
1. ARITHMETIC OPERATORS
2. RELATIONAL OPERATORS
3. LOGICAL OPERATORS
4. ASSIGNMENT OPERATORS
5. MISCELLANEOUS OPERATORS
OPERATORS
ARITHMETIC
Operator
OPERATORS Description Example Result
+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
/ Division 6-2 3
^ or ** Exponentiation 2^3 8
%% Modulus (Remainder) 10 %% 3 1
%/% Integer Division 10 %/% 3 3
- (Unary) Unary Minus (Negation) -5 -5
%*% Matrix Multiplication M %*% N Result
OPERATORS
RELATIONAL OPERATORS
Operator Description Example Result
< Less than 5 < 10 TRUE
<= Less than or equal to 5 <= 10 TRUE
> Greater than 5 > 10 FALSE
>= Greater than or equal to 5 >= 10 FALSE
== Equal to 5 == 5 TRUE
!= Not equal to 5 != 10 TRUE
%in% Value exists in a vector 5 %in% c(1, 2, 3, 4, 5) TRUE
! Negation (not) !(5 == 10) TRUE
OPERATORS
LOGICAL OPERATORS
Operator Description Example Result
& Logical AND TRUE & FALSE FALSE
&& Short-circuit AND TRUE && FALSE FALSE
| Logical OR TRUE | FALSE TRUE
|| Short-circuit OR TRUE || FALSE TRUE
! Logical NOT !TRUE FALSE
Exclusive OR
xor() xor(TRUE, FALSE) TRUE
(XOR)
Check if object is
isTRUE() isTRUE(5 > 3) TRUE
TRUE
Check if any are
any() any(c(FALSE, FALSE, TRUE)) TRUE
TRUE
Check if all are
all() all(c(TRUE, TRUE, FALSE)) FALSE
OPERATORS
ASSIGNMENT OPERATORS
Operato
r Description Example Result
Standard assignment operator. Assigns a
<- or = x <- 5 or x = 5 x contains 5
value to a variable.
raw numeric
Data
Type
s
complex integer
logical
DATA TYPES
Data Type Syntax Description
Syntax: Example:
if (condition) { x <- 10
# Code to execute if if (x > 5) {
condition is TRUE print("x is greater than
} 5")
}
CONDITIONAL STATEMENTS
IF-ELSE STATEMENT:
THE IF-ELSE STATEMENT ALLOWS YOU TO EXECUTE ONE BLOCK
OF CODE IF A CONDITION IS TRUE AND ANOTHER BLOCK IF IT IS
FALSE.
Syntax: Example:
if (condition) { x <- 3
# Code to execute if if (x > 5) {
condition is TRUE print("x is greater than 5")
} else { } else {
# Code to execute if print("x is not greater than
condition is FALSE 5")
} }
CONDITIONAL STATEMENTS
IF-ELSE IF-ELSE STATEMENT:
Syntax: Example:
for (variable in sequence) for (i in 1:5)
{ {
# Code to execute for
each iteration
print(paste("Iteratio
} n", i))
}
LOOPS
WHILE LOOP:
Syntax: Example:
while (condition) { x <- 1
# Code to execute while while (x <= 5) {
the condition is TRUE print(paste("x is", x))
} x <- x + 1
}
LOOPS
REPEAT LOOP:
String Manipulation
R provides various functions for manipulating strings,
such as paste(), substr(), toupper(), tolower(), and gsub(),
among others.
STRINGS
String Length
Determine the length of a string using the nchar()
function.
• INDEXING
• BUILTIN FUNCTIONS
• USER INPUT
ASSIGNMENT - 1
1. Create a function that checks if a given integer is even or odd. Print an appropriate message based on the
result.
2. Write a program that defines a function to calculate and print the sum of two user-input numbers.
3. Create a function to compute and return the factorial of a given positive integer. Test the function with
different input values.
4. Develop a function that converts temperatures between Celsius and Fahrenheit. Allow the user to choose
the conversion direction and input the temperature value.
5. Write a program that defines a function to calculate and return the mean, median, mode, min and max of a
numeric vector provided as input.
6. Define a function to generate the first 'n' terms of the Fibonacci sequence and print the sequence.
7. Write a function that checks if a given string is a palindrome (reads the same forwards and backwards).
Return TRUE or FALSE based on the result.
8. Develop a function to calculate simple interest and compound interest (monthly, quarterly and yearly)
based on user-input principal amount A, rate R, and time T.
9. Define a function that counts the number of words in a given sentence or string. Exclude punctuation and
consider spaces as word separators.
10. Develop a program that calculates and prints the Equated Monthly Installment (EMI) for a loan. Allow
users to input loan amount, interest rate, and loan tenure.
• 50 MARKS FOR MTE AND 50 MARKS FOR ETE
• DECIDE A DATE FOR MID TERM PRACTICAL EXAM IN FIRST WEEK OF OCTOBER