Lab 5B – Data Import
1. Write a program to access and print the following data included in R:
a. AirPassengers in datasets package.
b. Economics in ggplot2 package
Code
library(datasets)
library(ggplot2)
print(AirPassengers)
print(economics)
Output
2. Create a text file manually and save the following content in it
12000
7000
9000
6000
8000
Then, read this file and print the minimum, maximum, and average values.
Code
#dir path
setwd("F:/")
#read data
data<- scan("number.txt")
data
max(data)
min(data)
mean(data)
Output
3. Write an R program to read the content of a CSV file located in the following link
http://www.jaredlander.com/data/TomatoFirst.csv . Then, use the obtained data to
achieve the following tasks:
Code
install.packages("readr")
library(readr)
data<-read.csv("C:\\Users\\Rajesh\\Downloads\\archive\\TomatoFirst.csv")
data
Output
a. Return how many rows and columns in this file.
Code
cat("no. of rows",nrow(data))
cat("no. of columns",ncol(data))
Output
b. Return the details of the least Sweet tomato type.
Code
minsweet<- min(data$Sweet)
minsweet
Output
c. Return the summation of prices when the Source is “Whole Foods”.
Code
leastsweettomato<- data[data$Sweet == minsweet,]
leastsweettomato
Output
4. Write an R program to read the content of third sheet in an excel file located in the
following link http://www.jaredlander.com/data/ExcelExample.xlsx .
Code
filepath <- ("C:/Users/admin/Downloads/ExcelExample.xlsx")
sheet3 <- read_excel(filepath,sheet = "ACS")
sheet3
sheet1 <- read_excel(filepath, sheet = "Tomato")
sheet1
sheet2 <- read_excel(filepath, sheet = "Wine")
sheet2
Output
5. Write an R program to download an SQLite database located in the following link
http://www.jaredlander.com/data/diamonds.db . Then, do the following tasks:
a. Connect to this dataset. Then list the tables in this dataset and the fields in each
table.
b. Print the content found in diamonds table.
Code
install.packages("RSQLite")
library(RSQLite)
url<- "http://www.jaredlander.com/data/diamonds.db"
download.file(url,destfile = "diamond.db",mode = "wb")
con<- dbConnect(SQLite(),dbname="diamonds.db")
dbListResults(con)
#fields
tables< dbListFields(con)
for (table in tables) {
fields<- dbListFields(con,table)
cat(paste0("Fields in table",table,":",
paste(fields,collapse=",","\n")))
}
diamondsdata<- dbReadTable(con,"diamonds")
diamondsdata
dbDisconnect(con)
Conclusion
This R program involved the import and analysis of various datasets using different
packages. Two datasets, namely AirPassengers and economics, were imported from the datasets
library. These datasets provided information on monthly airline passenger numbers and
economic indicators, respectively.
The program then proceeded to perform several tasks. In Question 2, it read a text file named
"number.txt" and calculated basic statistics such as the maximum, minimum, and mean values.
The calculated statistics were then displayed.
Question 3 focused on data manipulation using the readr package. The program read a CSV
file called "TomatoFirst.csv" and determined the number of rows and columns in the dataset.
Additionally, it identified the tomato with the least sweetness by finding the minimum value in
the "Sweet" column.
Moving on to Question 4, the program worked with Excel files using the readxl package. It
read an Excel file named "ExcelExample.xlsx" and extracted data from multiple sheets,
including "ACS", "Tomato", and "Wine". The data from each sheet was then printed for analysis.
Finally, in Question 5, the program utilized the RSQLite package to download an SQLite
database file named "diamonds.db" from a specified URL. It established a connection to the
database, listed the tables and fields within it, and retrieved and printed the data from the
"diamonds" table. The database connection was subsequently closed.
Overall, this R program showcased the import, manipulation, and analysis of data using
various packages. It demonstrated the capability to work with different file formats and
databases.