0% found this document useful (0 votes)
13 views9 pages

Ecotrix Codes p2

Uploaded by

zuhanshaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Ecotrix Codes p2

Uploaded by

zuhanshaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PERIODICAL-2

Zuhan Shaikh- BECO21268

A1)
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","F
ebruary","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)
)
city_data

library(tidyr)
city_datalong<-pivot_longer(data=city_data,cols=c("temperature","precipitation"))
A2
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)
)
sales_data

library(reshape2)
sales_datawide<-pivot_wider(sales_data,names_from = month, values_from = c(sales,profits) )

A3
sales_data2 <- 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))

totalsalesA = sum(sales_data2 %>% filter(product == "A") %>% select(sales))


totalprofitsA = sum(sales_data2 %>% filter(product == "A") %>% select(profits))
print(paste("Product A's total sales are", totalsalesA,
"and total profits are", totalprofitsA))

totalsalesB = sum(sales_data2 %>% filter(product == "B") %>% select(sales))


totalprofitsB = sum(sales_data2 %>% filter(product == "B") %>% select(profits))
print(paste("Product B's total sales are", totalsalesB,
"and total profits are", totalprofitsB))

totalsalesC = sum(sales_data2 %>% filter(product == "C") %>% select(sales))


totalprofitsC = sum(sales_data2 %>% filter(product == "C") %>% select(profits))
print(paste("Product C's total sales are", totalsalesC,
"and total profits are", totalprofitsC))

DATA = sales_data %>%


group_by(product) %>%
summarise(totalprofits=sum(profits))
DATA %>% arrange(desc(totalprofits))
A4

employee_data<- data.frame(
employee = c("Alice","Bob","Charlie","Dave"),
hours = c(40, 35, 30, 45),
wage = c(20, 25, 18, 22)
)
employee_data
Total_earnings<-data.frame(total_earnings=employee_data$hours*employee_data$wage)
Total_earnings
cbind(employee_data,Total_earnings)
A5)

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

mean_temp<-aggregate(weather_data$temperature, by=list(city=weather_data$city),
FUN=mean)
mean_temp

weather_data %>%
group_by(city) %>%
summarise(meantemp=mean(temperature))

mean_prec<-aggregate(weather_data$precipitation,
by=list(city=weather_data$city),FUN=mean)
mean_prec

weather_data %>%
group_by(city) %>%
summarise(meanprec=mean(precipitation))
A6)

library(dplyr)
library(ggplot2)
library(tidyverse)
library(haven)

nations = read_dta ("C:/Users/Zuhan/Downloads/Nations2.dta")


View(nations)

colnames(nations)
summary(nations)

independent_vars = c("country", "region", "pop", "urban",


"femlab", "co2","chldmort","adfert", "school","literacy")
dependent_vars = c("life", "gini","gdp")

scipen = 999

ggplot(nations, aes(x=gini, y=education) + geom_point() + geom_smooth (method = lm)

hist(nations$gdp)

reg1 = lm(gdp ~ co2 + urban + life, data = nations)


summary (reg1)

reg2 = lm(gini ~ school + literacy + adfert, data = nations)


summary (reg2)

You might also like