0% found this document useful (0 votes)
5 views15 pages

R Programming Data Handling Techniques

The document is a lab journal detailing various R programming exercises, including prime number generation, matrix multiplication, text analysis, random number generation, and string manipulation. Each exercise includes an aim, algorithm, source code, error reflections, and application areas, with all programs executed successfully. The journal emphasizes the creation and manipulation of different data types and structures in R.
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)
5 views15 pages

R Programming Data Handling Techniques

The document is a lab journal detailing various R programming exercises, including prime number generation, matrix multiplication, text analysis, random number generation, and string manipulation. Each exercise includes an aim, algorithm, source code, error reflections, and application areas, with all programs executed successfully. The journal emphasizes the creation and manipulation of different data types and structures in R.
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

Lab Journal

Date: Title: Prime Number Generation [Link]. 1

Aim:
To create and display different types of data objects in R programming.
Expected Input:

Expected Output:

Algorithm:
1. Start the program.
2. Numeric variables are declared and assigned suitable integer values for basic arithmetic
operations.
3. Character variables are created and assigned text values using double quotation marks.
4. Logical variables are defined and assigned TRUE and FALSE values.
5. Arithmetic operation is performed using numeric variables and result is stored.
6. Numeric variable values are displayed using print function.
7. Character variable values are displayed to verify correct assignment.
8. Logical values are displayed to confirm Boolean data handling.
9. All created variables are checked for correct data type representation.
10. The output is verified for accuracy and proper execution.
11. The program execution is completed after displaying all results.
12. stop the program

Page No.
Lab Journal

Source Code:
a <- 10
b <- 20
sum <- a + b

name <- "Suryaa"


college <- "Don Bosco"

flag1 <- TRUE


flag2 <- FALSE

print(a)
print(b)
print(sum)
print(name)
print(college)
print(flag1)
print(flag2)
}

Page No.
Lab Journal

Error Reflections:

Errors Observed How Were They Fixed?

Errors occurred due to missing quotation


marks for character data.
The issue was fixed by enclosing text
values within double quotes.

Where will you apply this code?


 Used to understand basic variable creation and data types in R.
 Applied in simple calculations and initial data analysis tasks.

Remarks by the Subject handler:

Result:
This program has been executed successfully.

Page No.
Lab Journal

Date: Title: Matrix Multiplication [Link]. 2

Aim:
To read data from a CSV file and write data into another file using R.

Expected Input:

Expected Output:

Algorithm:
1. Start the program.
2. The working directory is set to access the required input and output files.
3. The input CSV file name is specified for reading data.
4. The CSV file is read using the appropriate R function.
5. File contents are stored into a data frame variable.
6. The stored data is displayed to verify successful reading.
7. The number of rows and columns are identified from the data.
8. An output file name is specified for writing data.
9. The data frame is written into a new CSV file.
10. File writing operation is verified through confirmation message.
11. Successful completion of file reading and writing is ensured.
12. Stop the [Link] input
CSV file name is specified for reading data.
4. The CSV file is read using the appropriate R function.
5. File contents are stored into a data frame variable.
6. The stored data is displayed to verify successful reading.

Page No.
Lab Journal

Source Code:
setwd("D:/Rfiles")

input_data <- [Link]("[Link]")


print(input_data)

rows <- nrow(input_data)


cols <- ncol(input_data)

print(rows)
print(cols)

output_data <- input_data


[Link](output_data, "[Link]")

print("File written successfully")

Page No.
Lab Journal

Error Reflections:

Errors Observed How Were They Fixed?

File not found error occurred due to


incorrect directory path.
The error was resolved by setting the
correct working directory

Where will you apply this code?


 Used to import datasets from files for analysis in R.
 Applied to export processed data for reports and storage

Remarks by the Subject handler:

Result:
This program has been executed successfully.

Page No.
Lab Journal

Date: Title: Text Analysis Program [Link]. 3

Aim:
To connect R with a database and retrieve records using SQL queries.

Expected Input:

Expected Output:

Algorithm:
1. Start the program.
2. Required database interface library is loaded into the R environment.
3. A connection is established between R and the database file.
4. The database connection object is stored for further operations.
5. An SQL query is defined to retrieve records from a table.
6. The SQL query is executed using the database connection.
7. Retrieved records are stored into a data frame.
8. The fetched data is displayed for verification.9. The total number of retrieved records is
calculated.
10. The database connection is safely closed.
11. Database resources are released properly.
12. Stop the program.

Page No.
Lab Journal

Source Code:
library(DBI)

con <- dbConnect(RSQLite::SQLite(), "[Link]")

query <- "SELECT * FROM student"


result <- dbGetQuery(con, query)

print(result)

count <- nrow(result)


print(count)

dbDisconnect(con)
print("Connection closed")");
}
}

Page No.
Lab Journal

Error Reflections:

Errors Observed How Were They Fixed?

Connection error occurred due to missing


package.
It was fixed by installing and loading the
required library.

Where will you apply this code?


 Used to retrieve data from databases using R.
 Applied in data analytics and database-driven applications

Remarks by the Subject handler:

Result:
This program has been executed successfully.

Page No.
Lab Journal

Date: Title: Random Number Generation [Link]. 4

Aim:
To create and manipulate date values using R programming.
Expected Input:

Expected Output:

(OR)

(OR)

Algorithm:
1. Start the program.
2. A date value is defined in character format.
3. The character date is converted into Date object format.
4. The current system date is obtained from the system.
5. Both date values are stored in separate variables.
6. The stored dates are displayed for verification.
7. Difference between two dates is calculated.
8. The calculated date difference is stored.
9. Year, month, and day values are extracted from the system date.
10. All date-related results are displayed successfully.
11. The correctness of date operations is verified.
12. Stop the program.

Page No.
Lab Journal

Source Code:
date1 <- [Link]("2025-01-01")
date2 <- [Link]()

print(date1)
print(date2)

diff <- date2 - date1


print(diff)

year <- format(date2, "%Y")


month <- format(date2, "%m")
day <- format(date2, "%d")

print(year)
print(month)
print(day)
}

Page No.
Lab Journal

Error Reflections:

Errors Observed How Were They Fixed?

Date format error occurred due to invalid


input format.
It was corrected by using the YYYY-MM-
DD format.

Where will you apply this code?


 Used in applications requiring date and time calculations.
 Applied in attendance systems and time-based analysis.

Remarks by the Subject handler:

Result:
This program has been executed successfully.

Page No.
Lab Journal

Date: [Link]. 5
Title: String Manipulation Using Character
Arrays

Aim:
To create and analyze categorical data using factors in R.
Expected Input:

Expected Output:

Algorithm:
1. Start the program.
2. A character vector is created with categorical values.
3. The character vector is converted into a factor.
4. Factor levels are automatically assigned by R.
5. The factor values are displayed for verification.
6. The levels of the factor are displayed.
7. Frequency of each category is calculated.
8. The frequency table is displayed.
9. Summary of factor data is obtained.
10. Factor structure is verified using built-in functions.
11. Correct representation of categorical data is ensured.
12. Stop the program.

Page No.
Lab Journal

Source Code:
gender <- c("Male", "Female", "Male", "Female", "Male")

f_gender <- factor(gender)

print(f_gender)
print(levels(f_gender))

count <- table(f_gender)


print(count)

summary(f_gender)
[Link](f_gender)

Page No.
Lab Journal

Error Reflections:

Errors Observed How Were They Fixed?

Incorrect output occurred when factor


conversion was skipped.
The issue was fixed using the factor()
function.

Where will you apply this code?


 Used to represent categorical data in statistical analysis.
 Applied in surveys and machine learning preprocessing.

Remarks by the Subject handler:

Result:
This program has been executed successfully.

Page No.

You might also like