Вы находитесь на странице: 1из 5

Installation

library(devtools)
library(rpivotTable)
install_github("ramnathv/htmlwidgets")
install_github("smartinsightsfromdata/rpivotTable")

-----------------------------------------------------------------------------------
--------------------

library(devtool)
library(rpivotTable)
espn = read.csv("espn.CSV",header=TRUE)
rpivotTable(espn, rows="Gender", col="Location", aggregatorName="Average",
vals="Income", rendererName="Table")

library(devtool)
library(rpivotTable)
product=read.csv("pivot-table.CSV",header=TRUE)
rpivotTable(product, rows="Product", col="Catgeory",
aggregatorName="Average",vals="Amount", rendererName="Table")

Case1

Q1.
library(devtools)
library(rpivotTable)
espn = read.csv("espn.CSV",header=TRUE)
rpivotTable(espn, row="Gender", aggregatorName="Count as Fraction of
Total",rendererName="Table")

Q2
rpivotTable(espn, row="Age", aggregatorName="Count as Fraction of Total",
vals="Age", rendererName="Table")

Q3.
rpivotTable(espn, row="Location", col ="Gender", aggregatorName="Count",
rendererName="Table")

Q4.
rpivotTable(espn, col="Location", aggregatorName="Count as Fraction of
Total",rendererName="Table")

Q5.
rpivotTable(espn, col="Income", rows="Age" ,aggregatorName="Count as Fraction of
Total", vals="Location", rendererName="Table")
Case 2(pivot-table)
Q1.

Q2.

Q3.

Q4.

Q5.

-----------------------------------------------------------------------------------
-----------------------------------------
For using melt and cast function

after importing pivot_table


use the following @console

pivot_table.m <- melt(pivot_table, id=c(1:3,5:6), measure=c(4))


pivot_table.m

1. Question: Which is the most exported product?

pivot_table.c <- cast(pivot_table.m, Product ~ variable, sum)


pivot_table.c

2.Question: What is the total amount of exports?

pivot_table.c <- cast(pivot_table.m, Product ~ variable, sum,


margins=c("grand_row"))
pivot_table.c

3.Question: Which product do we export most to United State?


pivot_table.c <- cast(pivot_table.m, Product ~ variable, sum,
margins=c("grand_row"), subset=(Country=="United States"))
pivot_table.c

4.Question: What the average export amount to United States?


pivot_table.c <-cast(pivot_table.m, Product ~ variable, margins = c("grand_row"),
mean, subset=(Country== "United States") )
pivot_table.c

5.Question: What the average export amount to United States for both category?
pivot_table.c <-cast(pivot_table.m,Category ~ variable,margins = c("grand_row"),
mean, subset=(Country== "United States") )
pivot_table.c

6.Question: Category wise total and grand total


pivot_table.c <-cast(pivot_table.m,Category ~ variable,sum,margins =
c("grand_row"))
pivot_table.c

7.Question: What are the export percentage country wise?


pivot_table.c <- cast(pivot_table.m, Country ~ variable, sum)
pivot_table.c
pivot_table.c$Amount_pct <- pivot_table.c$Amount / sum(pivot_table.c$Amount)
#Calculates the percentage of amounts
pivot_table.c

8.Question: What is the frequency of export to different countries?


pivot_table.c <- cast(pivot_table.m, Product ~ variable, margins=c("grand_row"))
pivot_table.c

9.Question:What are the exports category and country wise total ?


pivot_table.c <-cast(pivot_table.m, Category+Country ~ variable, margins =
c("grand_row"), sum)
pivot_table.c

if you want to display category country and also product in above question then
pivot_table.c <-cast(pivot_table.m, Category+Country+Product ~ variable, margins =
c("grand_row"), sum)
pivot_table.c

10.Question: Product wise analysis with country ?


pivot_table.c <-cast(pivot_table.m, Product+Country ~ variable, margins =
c("grand_row"), sum)
pivot_table.c

11.Question: Compare the amount of Apple export to Australia and Canada


pivot_table$value <- pivot_table$Amount #This helps creating a two-
dimensional pivot table. The value column gives the amounts between each pair of
variables
cast(pivot_table, Country ~ Product, sum) # Analyzes the amounts between
each pair of countries and products

12.Question: Compare the number of Apple exports to Australia and Canada


cast(pivot_table, Country ~ Product, length) # Analyzes the number of exports
(count) between each pair of countries and products

13.Question: Compare the average amount of Apple exports to Australia and Canada
cast(pivot_table, Country ~ Product, mean) # Analyzes the means (average amounts)
between each pair of countries and products

14. to create csv file


pivot_table.c <- cast(pivot_table,Country ~ Product,sum)
write.csv(pivot_table.c,"pivot_countryProduct.csv")
------------------------------------------------------------------------------
case(espn)
(solve your self using melt and cast function)

-----------------------------------------------------------------------------------
-
MUltiple tables analysis

library(readxl)
> Data <- read_excel("C:/Users/PRS/Desktop/merger/Data.xlsx")
> View(Data)

>library(readxl)
> Dept <- read_excel("C:/Users/PRS/Desktop/merger/Dept.xlsx")
> View(Dept)

> library(readxl)
> Faculty <- read_excel("C:/Users/PRS/Desktop/merger/Faculty.xlsx")
> View(Faculty)

View(dept)
head(dept)
m1=merge(Data,Dept,by="Dept. Code")

write.csv(m1, file = "tab1.csv")

m3=merge(m1,Faculty,by="Faculty Code") /*correlating third table in resultant


table*/

write.csv(m3, file = "tab3.csv")


-----------------------------------------------------------------
Q1.Calculate Department wise total salry

library(devtools)

library(rpivotTable)

pivot2=read.csv("tab3.csv",header=TRUE)

rpivotTable(pivot2, rows="Name", aggregatorName="Sum", vals="Salary",


rendererName="Table")

Q2.Calculate Level wise salary total?

rpivotTable(pivot2, rows="Level", aggregatorName="Sum", vals="Salary",


rendererName="Table")

Q3.Calculate Department wise and Level wise Salary total?

rpivotTable(pivot2, rows="Name", col ="Level",aggregatorName="Sum", vals="Salary",


rendererName="Table")

Q4.Calculate travel expenses by each department?

rpivotTable(pivot2, rows="Name", aggregatorName="Sum", vals="Travel.expenses",


rendererName="Table")

-----------------------------------------------------------------------------------
-----------------------
different file format
-----------------------------------------------------------------------------------
---------------------
for importing txt file

data2 <- read.delim(file.choose(), header=T)


View(data2)
library(readxl)

now import xlsx file from menue

states <- read_excel("C:/Users/PRS/Desktop/merger/states.xlsx")


> View(states)

> 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")

FOR BINARY EXCEL FILE


install.packages("excel.link")
library("excel.link")
df<-xl.read.file('rand.xlsb',header=TRUE)