Pranav R Programming Lab File
Pranav R Programming Lab File
LAB MANUAL
Subject code :- ACTDSRPR001P
SUBJECT NAME :- R Programming Lab
SEMESTER: VI SEM III YEAR
Session: - Dec - may 2023
2.
VECTORS
3.
CONTROL STATEMENTS
4.
FUNCTIONS IN R
5.
MATRICES
6.
STRINGS
7.
LISTS
8.
ARRAYS IN R
9.
R FACTORS
10.
DATA FRAMES IN R
Experiment 1
FUNDAMENTALS OF R
easily be created by R.
packages.
in the Data Science job market which makes it the hottest trend
nowadays
Why Use R?
● Statistical Analysis: R is designed for analysis and It provides an
visualizations.
other tools.
that extend its functionality. There are packages that can help
packages.
Statistical Features of R
● Basic Statistics: The most common basic statistics terms are the
Basic R program
Since R is much similar to other widely used languages syntactically, it is
easier to code and learn in R. Programs can be written in R in any of the
widely used IDE like R Studio, Rattle, Tinn-R, etc. After writing the
program save the file with the extension .r. To run the program use the
following command on the command line:
R file_name.r
R
Output:
Welcome to GFG!
Advantages of R
● R is the most comprehensive statistical analysis package. As new
system.
Disadvantages of R
● In the R programming language, the standard of some packages
available memory.
Applications of R
● We use R for Data Science. It gives us a broad variety of libraries
R Vectors
Vectors
A vector is simply a list of items that are of the same type.
To combine the list of items to a vector, use the c() function and separate
the items by a comma.
In the example below, we create a vector variable called fruits, that combine
strings:
Example
# Vector of strings
# Print fruits
fruits
Example
Numbers
Example
numbers
You can also create numerical values with decimals in a sequence, but note
that if the last element does not belong to the sequence, it is not used:
Example
numbers1
Result:
Example
log_values
Vector Length
To find out how many items a vector has, use the length() function:
Example
length(fruits)
Sort a Vector
To sort items in a vector alphabetically or numerically, use the sort()
function:
Example
Access Vectors
You can access the vector 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
fruits[1]
Example
fruits[c(1, 3)]
You can also use negative index numbers to access all items except the ones
specified:
Example
fruits[c(-1)]
Change an Item
To change the value of a specific item, refer to the index number:
Example
# Print fruits
fruits
Repeat Vectors
To repeat vectors, use the rep() function:
Example
Repeat_each
Example
Repeat_times
Example
repeat_indepent
Numbers
Example
Numbers
Experiment 3
CONTROL STATEMENTS
In R, control statements are used to control the flow of execution in a program. The main
control statements in R include:
```r
if (condition) {
} else {
```
```r
if (condition1) {
} else if (condition2) {
} else {
```
3. **for:** It is used to iterate over a sequence (like a vector, list, or data frame) and execute
a block of code for each element in the sequence.
```r
# code to be executed
```
```r
while (condition) {
# code to be executed
```
```r
repeat {
# code to be executed
if (condition) {
break
}
}
```
7. **next:** It is used to skip the rest of the current iteration of a loop and move to the next
iteration.
8. **switch:** It is used to select one of several blocks of code to execute based on the value
of an expression.
```r
switch(expression,
value1 = {
},
value2 = {
},
...
default = {
```
These control statements allow you to implement conditional logic, looping, and branching in
R programs, enabling you to write more complex and flexible code.
Experiment 4
FUNCTIONS IN R
In R, functions are blocks of code that perform a specific task and can be reused throughout your
script or session. They take input arguments, perform operations, and return output values. Here's how
you define and use functions in R:
You can define functions in R using the `function` keyword. The basic syntax is as follows:
```r
return(output)
```
### Example:
Let's define a simple function that calculates the sum of two numbers:
```r
result <- a + b
return(result)
}
```
You can call a function by its name and pass arguments to it:
```r
print(result) # Output: 8
```
Functions in R can have both positional and named arguments. Named arguments allow you to
specify the order of arguments explicitly:
```r
print(result) # Output: 8
```
You can assign default values to function arguments, allowing users to omit them if desired:
```r
result <- a + b
return(result)
}
result <- sum_numbers(5)
print(result) # Output: 5
```
```r
return(result)
print(result) # Output: 3
```
Functions return values using the `return()` statement. If no `return()` statement is present, the
function returns the last evaluated expression:
```r
a+b
print(result) # Output: 8
```
### Documentation:
Document your functions using comments and the `#'` syntax. You can then use `?function_name` or
`help(function_name)` to access the documentation.
```r
#'
result <- a + b
return(result)
```
Functions are essential for structuring your code, promoting code reuse, and making your scripts more
readable and maintainable.
Experiment 5
MATRICES
In R, matrices are two-dimensional arrays that contain elements of the same data type. They are useful
for storing and manipulating data in a tabular format, such as in mathematical operations, statistical
analysis, and data manipulation tasks. Here's how you work with matrices in R:
You can create matrices in R using the `matrix()` function. The basic syntax is as follows:
```r
```
- `byrow`: A logical value indicating whether the matrix should be filled by rows (`TRUE`) or by
columns (`FALSE`). Default is `FALSE`.
- `dimnames`: Optional list providing names for the rows and columns.
### Example:
```r
print(mat)
```
### Accessing Elements:
You can access elements of a matrix using square brackets `[ ]` with row and column indices:
```r
print(element) # Output: 6
```
### Operations:
Matrices support various arithmetic and matrix operations, such as addition, subtraction,
multiplication, and transposition:
```r
# Matrix operations
# Addition
# Subtraction
# Element-wise multiplication
# Matrix multiplication
result_matmul <- mat1 %*% t(mat2) # Matrix multiplication requires transposing one of the
matrices
# Transposition
```
You can perform operations on rows and columns using functions like `rowSums()`, `colSums()`,
`rowMeans()`, and `colMeans()`:
```r
```
You can bind matrices together horizontally or vertically using `cbind()` and `rbind()` functions:
```r
# Binding matrices
```
```r
# Manipulating matrices
dimnames(mat) <- list(c("row1", "row2", "row3"), c("col1", "col2", "col3")) # Set row and
column names
```
Matrices are fundamental data structures in R and are widely used in various statistical and
computational tasks. Understanding how to create, manipulate, and perform operations on matrices is
essential for data analysis and manipulation in R.
Experiment 6
STRINGS
In R, "strings" typically refer to character vectors, which are sequences of characters. Here's a brief
overview of how you can work with strings in R:
1. **Creating Strings**: You can create strings using single quotes (`'`) or double quotes (`"`). For
example:
```R
```
2. **Concatenating Strings**: You can concatenate strings using the `paste()` function or the
`paste0()` function:
```R
```
3. **String Manipulation**: There are several functions for manipulating strings in R, such as:
4. **Regular Expressions**: R supports regular expressions for advanced string manipulation tasks.
Functions like `grep()`, `grepl()`, `regexpr()`, and `sub()` allow you to work with regular expressions.
5. **String Comparison**: You can compare strings using operators like `==`, `!=`, `<`, `>`, etc. Keep
in mind that string comparison is case-sensitive by default.
6. **String Searching**: Functions like `grep()` and `grepl()` can be used for searching strings and
finding patterns within them.
7. **String Encoding**: R supports different character encodings, and you can convert between them
using functions like `iconv()`.
8. **String Operations**: R provides various functions for operations like trimming whitespace
(`trimws()`), padding strings (`str_pad()`), and finding substrings (`str_detect()`, `str_subset()`,
`str_extract()`, etc.) through the `stringr` package.
```R
# Create strings
# Concatenate strings
# String manipulation
# Regular expressions
print(combined)
print(uppercase)
print(substring)
print(contains_W)
```
This code will output the concatenated string, the uppercase version of it, a substring of the first 5
characters, and whether the string contains the letter "W".
Experiment 7
LISTS
In R, a list is a data structure that can hold elements of different types such as vectors, matrices, data
frames, or even other lists. Lists are quite flexible and useful for organizing and storing heterogeneous
data. Here's an overview of how to work with lists in R:
You can create a list using the `list()` function, specifying the elements you want to include:
```R
# Create a list
my_list <- list(name = "John", age = 30, grades = c(85, 90, 95))
```
You can access elements of a list using double brackets `[[ ]]` or by using the dollar sign `$` followed
by the element name:
```R
# Access elements
```
You can add elements to a list using the list subsetting notation or by using the `append()` function:
```R
```
### Length and Structure:
You can find the length of a list using the `length()` function and view its structure using the `str()`
function:
```R
# Length of list
# Structure of list
str(my_list)
```
You can manipulate lists in various ways, such as extracting subsets, combining lists, or removing
elements:
```R
# Combining lists
# Removing elements
```
You can create nested lists, which are lists that contain other lists:
```R
# Nested lists
```
Lists are powerful data structures in R, especially when dealing with complex and heterogeneous data.
They provide flexibility and ease of manipulation for various analytical tasks.
Experiment 8
ARRAYS IN R
In R, arrays are multidimensional data structures that can hold elements of the same data type. Arrays
can have one or more dimensions, and they are particularly useful for representing data in multiple
dimensions, such as matrices or higher-dimensional data. Here's an overview of how to work with
arrays in R:
You can create arrays using the `array()` function, specifying the data elements and dimensions:
```R
```
You can also create arrays from vectors using the `dim()` function:
```R
```
You can access elements of an array using indexing for each dimension:
```R
# Access elements
element <- my_array[1, 2] # Accesses element in the first row, second column
```
```R
# Dimensions of array
# Structure of array
str(my_array)
```
You can manipulate arrays in various ways, such as reshaping, transposing, or combining them:
```R
# Reshaping array
# Transposing array
# Combining arrays
```
You can perform various operations on arrays, such as element-wise arithmetic operations:
```R
# Element-wise arithmetic
You can create arrays with more than two dimensions, which are often used in applications like image
processing or simulations:
```R
# Create a 3D array
```
Arrays are versatile data structures in R, particularly useful for handling multidimensional data sets.
They provide efficient storage and manipulation capabilities for complex data analysis tasks.
Experiment 9
R FACTORS
In R, a factor is a data type used for categorical variables. Factors are useful for representing data that
have a fixed number of unique values or levels, such as "male" and "female" for gender or "low",
"medium", and "high" for levels of education.
```R
# Create a factor
```
By default, R will assign levels to the factor based on the unique values in the data. You can also
specify the levels explicitly:
```R
```
You can view the levels of a factor using the `levels()` function:
```R
# View levels
```
You can get a summary of the factor using the `summary()` function:
```R
# Summary of factor
summary(gender)
```
You can convert character vectors to factors using the `as.factor()` function:
```R
# Convert to factor
```
You can change the levels of a factor using the `levels()` function:
```R
```
You can remove unused levels from a factor using the `droplevels()` function:
```R
```
This can be useful when subsetting data and you want to remove levels that are not present in the
subset.
Factors play an essential role in statistical modeling and data analysis in R, particularly for
representing categorical variables in a structured and efficient way.
Experiment 10
DATA FRAMES IN R
In R, a data frame is a two-dimensional data structure similar to a table or spreadsheet in which data is
organized into rows and columns. Data frames are one of the most commonly used data structures in
R for handling and manipulating structured data. Here's an overview of working with data frames in
R:
You can create a data frame using the `data.frame()` function, specifying the columns as vectors:
```R
df <- data.frame(
```
You can view the contents of a data frame using the `print()` function or simply by typing its name:
```R
print(df)
```
You can access elements of a data frame using row and column indexes or by column names:
```R
# Access elements
```
You can get a summary of the data frame using the `summary()` function:
```R
summary(df)
```
This will provide a summary of each column in the data frame, including statistics such as mean,
median, min, max, and quartiles for numeric variables.
You can subset data frames to extract specific rows or columns using indexing:
```R
subset_df <- df[df$age > 30, ] # Select rows where age is greater than 30
```
```R
```
You can remove columns from a data frame using the `subset()` function:
```R
# Remove column
```
You can perform various operations on data frames such as merging, sorting, and reshaping using
functions like `merge()`, `order()`, and `reshape2` package functions like `melt()` and `dcast()`.
Factors are often used to represent categorical variables in data frames, as discussed in the previous
explanation.
Data frames are fundamental for data manipulation, exploration, and analysis in R, and mastering
their usage is essential for effective data handling and manipulation in R programming.