Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

ECONOMETRICS ASSIGNMENT P2

SECTION: A CODES

#Q1
city_data <- data.frame (city = c("Boston", "Boston", "Boston", "Boston", "New York", "New
York", "New York", "New York","Chicago", "Chicago", "Chicago", "Chicago", "San Francisco",
"San Francisco", "San Francisco", "San Francisco"), month = c("January", "February", "March",
"April", "January", "February", "March", "April", "January",
"February", "March", "April", "January", "February", "March", "April"), temperature = c(29,
33, 42, 54, 35, 38, 44, 57, 23, 27, 34, 47, 50, 52, 55, 58),
precipitation = c(3.4, 2.9, 4.1, 3.7, 3.8, 3.3, 4.0, 3.9, 2.1, 2.2, 2.8, 3.6, 4.4, 4.2, 4.0, 3.8))

library(tidyr)
city_data_long <- city_data %>%
pivot_longer(c("temperature", "precipitation"))
city_data_long

#Q2
sales_data <- data.frame(
product = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
region = c("East", "East", "East", "East", "East", "East", "West", "West", "West"),
month = c("January", "February", "March", "January", "February", "March", "January",
"February", "March"),
sales = c(100, 150, 200, 75, 125, 150, 50, 75, 100),
profits = c(25, 35, 50, 20, 30, 40, 10, 20, 30)
)

library(tidyr)
sales_data_wide= sales_data %>% pivot_wider(names_from = month , values_from = c(sales,
profits), id_cols = c(product,region))
sales_data_wide

#Q3
sales_data1 <- data.frame(
product = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D"),
region = c("East", "East", "East", "East", "East", "East", "East", "East", "West", "West", "West",
"West", "East", "East", "West", "West"),
month = c("January", "February", "March", "April", "January", "February", "March", "April",
"January", "February", "March", "April", "January", "February", "March", "April"),
sales = c(100, 150, 200, 250, 75, 125, 150, 175, 50, 75, 100, 125, 80, 120, 100, 150),
profits = c(25, 35, 50, 65, 20, 30, 40, 50, 10, 20, 30, 40, 15, 25, 20, 35)

A=sales_data1[1:4,]
B=sales_data1[5:8,]
C=sales_data1[9:12,]
D=sales_data1[13:16,]
A$sum_profits=sum(A$profits) #product wise sum
A$sum_sales=sum(A$sales)
B$sum_profits=sum(B$profits)
B$sum_sales=sum(B$sales)
C$sum_profits=sum(C$profits)
C$sum_sales=sum(C$sales)
D$sum_profits=sum(D$profits)
D$sum_sales=sum(D$sales)
sales_data2=rbind(A,B,C,D)
sales_data2= sales_data2[order(sales_data2$sum_profits, decreasing = TRUE),]

#Q3 (2)
library(dplyr)
sales_data3=select(sales_data1, product, sales, profits)
sales_data4= sales_data3 %>%
group_by(product) %>%
summarise(across(c(sales, profits) , sum))
colnames(sales_data4)[2:3]=c("total_sales", "total_profits")
sales_data4= sales_data4[order(sales_data4$total_profits, decreasing = TRUE),]

#Q4

employee_data <- data.frame(


employee = c("Alice", "Bob", "Charlie", "Dave"),
hours = c(40, 35, 30, 45),
wage = c(20, 25, 18, 22))

employee_data$earnings = (employee_data$hours * employee_data$wage)

#Q5
weather_data <- data.frame(
city = c("Boston", "Boston", "Boston", "Boston", "Boston", "New York", "New York", "New York",
"New York", "New York"),
date = c("2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-01",
"2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05"),
temperature = c(30, 32, 34, 36, 38, 35, 37, 39, 41, 43),
precipitation = c(2.0, 2.2, 2.5, 2.7, 2.9, 1.5, 1.7, 1.9, 2.1, 2.3)
)
boston= weather_data[1:5,]
ny=weather_data[6:10,]
boston$avg_temp=mean(boston$temperature)
boston$avg_precip=mean(boston$precipitation)
ny$avg_temp=mean(ny$temperature)
ny$avg_precip=mean(ny$precipitation)
weather_data1=rbind(boston,ny)

#Q5 (2)
library(dplyr)
weather_data3=select(weather_data, city, temperature, precipitation)
weather_data4= weather_data3 %>%
group_by(city) %>%
summarise(across(c(temperature, precipitation,) , mean))
colnames(weather_data4)[2:3]=c("avg_temp", "avg_precip")
weather_data4

You might also like