Professional Documents
Culture Documents
Session3 PDF
Session3 PDF
Today Objective
Similarly, to read .csv files the read.csv() function can be used to read
in the data frame directly
excle:
library("readxl")
airqual <-
read_excel("C:\\Users\\admin\\Desktop\\BA_Gradesheet.xl
sx") Indian Institute of Management (IIM),Rohtak
Pivot Table
espn = read.csv("espn.CSV",header=TRUE)
or espn = read.csv("espn.CSV")
rpivotTable(espn)
rpivotTable(espn, rows="Gender",
col="Location",
aggregatorName="Average",
vals="Income",
rendererName="Table")
Indian Institute of Management (IIM),Rohtak
Pivot Table
Case :espn.csv
Q1.Analyzing the gender of subscribers in
terms of percentage?
library(rpivotTable)
espn = read.csv("espn.CSV")
rpivotTable(espn, row="Gender",
aggregatorName="Count as Fraction of
Total",rendererName="Table")
library("readxl")
pivot_table <-read_excel("C:\\Users\\admin\\Desktop\\pivot-
table.xlsx")
ID
Measure
Indian Institute of Management (IIM),Rohtak
Melting the data
• When melting your data, you can indicate multiple id
variables as well as multiple measure variables
• melt transforms a data frame from the original format
to a so called long format, where all the observed
variables (called measures) appear, together with their
respective value, in two adjacent columns
named variable and value
• Each row of this new data format is identified by a
unique combination of the id variables, also part of
the original data frame
• The id variable will appear as is in the resulting
melted format, while the measure variable will be
stacked in the variable column with their respective
value in the value column
Indian Institute of Management (IIM),Rohtak
Melting the data
• pivot_table.m <- melt(pivot_table, id=c(1:3,5:6), measure=c(4))
• pivot_table.m
Filter criteria
Which product we export the most to United States?
>
pivot_table.c <-cast(pivot_table.m,Category
~ variable,sum,margins = c("grand_row"))
pivot_table.c
Category Amount
1 Fruit 693069
2 Vegetables 336665
3 (all) 1029734
Analyzes percentage
wise amount country
wise
2. Dept.xlsx
3.Faculty.xlsx
Questions:
Calculate Department wise Salary total?
Calculate Level wise salary total?
Calculate Department wise and Level
wise Salary total?
Calculate travel expenses by each
department?
MANY MORE …….
library("readxl")
data <-read_excel("C:\\Users\\admin\\Desktop\\data.xlsx")
IN CONSOLE
>library(readxl)
Now m1 you are going to write in another file name i.e tab1.csv
m3=merge(m1,Faculty,by="Faculty
Code") /*correlating third table in
resultant table*/
Questions:
Calculate Department wise Salary total?
Calculate Level wise salary total?
Calculate Department wise and Level
wise Salary total?
Calculate travel expenses by each
department?
MANY MORE …….
rpivotTable(pivot2, rows="Name",
aggregatorName="Sum", vals="Salary",
rendererName="Table")
rpivotTable(pivot2,
rows="Name", col
="Level",aggregatorName="Sum"
, vals="Salary",
rendererName="Table")
View(data2)
Indian Institute of Management (IIM),Rohtak
More than one Table
m4=merge(data2,states,by="Store")
write.csv(m4, file = "tab5.csv")
library(rpivotTable)
pivot3=read.csv("tab5.csv",header=TRUE)
rpivotTable(pivot3, rows="State", aggregatorName="Sum",
vals="Units", rendererName="Table")
Indian Institute of Management (IIM),Rohtak
More than one Table