#Creating a function with N as argument and computing the mean and standard deviation and returning this in a vector.
myfunc <- function(n)
{
myfunc <-c(mean(n),sd(n))
}
####################################
m1<-matrix(nrow=500,ncol=2)#Making a matrix or data frame for each experiment representing 10 samples.
m2<-matrix(nrow=500,ncol=2)#Making a matrix or data frame for each experiment representing 100 samples.
m3<-matrix(nrow=500,ncol=2)#Making a matrix or data frame for each experiment representing 10000 samples.
#####################################
# 500 experiments for mean value of nnorm(10)
for(i in 1:500)
{
m1[i,]<-myfunc(rnorm(10))
}
#########
# 500 experiments for mean value of nnorm(100)
for(i in 1:500)
{
m2[i,]<-myfunc(rnorm(100))
}
#########
# 500 experiments for mean value of nnorm(10000)
for(i in 1:500)
{
m3[i,]<-myfunc(rnorm(10000))
}
#########
min(min(m1[,1]),min(m2[,1]),min(m3[,1]))
## [1] -0.7723282
max(max(m1[,1]),max(m2[,1]),max(m3[,1]))
## [1] 0.9556174
min(min(m1[,2]),min(m2[,2]),min(m3[,2]))
## [1] 0.394775
max(max(m1[,2]),max(m2[,2]),max(m3[,2]))
## [1] 1.738755
# Plotting Hist for all the samples in the same scale.
hist(m1[,1],breaks=100,xlim=c(-2,2));
hist(m1[,2],breaks=100,xlim=c(-2,2));
hist(m2[,1],breaks=100,xlim=c(-2,2));
hist(m2[,2],breaks=100,xlim=c(-2,2));
hist(m3[,1],breaks=100,xlim=c(-2,2));
hist(m3[,2],breaks=100,xlim=c(-2,2));
library(readxl) #Using inbuilt function to read Excel.
## Warning: package 'readxl' was built under R version 3.6.2
Q2 <- read_excel("Q2.xlsx") #Reading file Q2 and assigning it to Q2.
library(readxl) #Using inbuilt function to read Excel.
Q2 <- read_excel("Q2.xlsx") #Reading file Q2 and assigning it to Q2.
with(Q2,plot(Y1, xlab='Republicans',xpd= TRUE,Y2,ylab = "Democrats")) # Plotting Scattered plot as Republicans v/s Democrats.
with(Q2,abline(lsfit(Y1,Y2),col="red"))
with(Q2,abline(line(Y1,Y2), col="blue"))
mtext(side=3, line=0.5, "Republicans v/s Democrats", col="forestgreen", font=3, cex=2)
with(Q2,text(Y1,Y2,labels = States))
# Plotting States v/s No. of Delegates
with(Q2,plot(1:56, xaxt = "n", xlab='States',xpd= TRUE, ylab = "No. of Delegates",Y1, col="red", pch = 17))
with(Q2,points(1:56, xaxt = "n", xlab='States', xpd= TRUE,ylab = "No. of Delegates",Y2, col="blue", pch = 17))
mtext(side=3, line=0.5, "States v/s No. of Delegates", col="forestgreen", font=3, cex=2)
labels=c('Alabama','Alaska','Amer. Samoa','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','DC','Florida','Georgia','Guam','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota', 'N. Marianas', 'Ohio','Oklahoma','Oregon','Pennsylvania','Puerto Rico','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virgin Islands','Virginia','Washington','West Virginia','Wisconsin','Wyoming')
abline(h=seq(0,700,20), v=seq(0,80, 1), lty=3, col="gray")
axis(1, at=1:56, labels=c('Alabama','Alaska','Amer. Samoa','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','DC','Florida','Georgia','Guam','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota', 'N. Marianas', 'Ohio','Oklahoma','Oregon','Pennsylvania','Puerto Rico','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virgin Islands','Virginia','Washington','West Virginia','Wisconsin','Wyoming'))
3. Filtering and Sorting
#Reading the data into R
senate.2014 <- read.csv("D:/MTU Course Files/SEM 1/ASAD/Problem Set 2/senate-2014.csv")
#Using the summary function
summary(senate.2014)
## FirstName LastName Affiliation AssumedOffice DOB Gender
## John : 5 Johnson : 2 D :50 Min. :1966 Min. :1933 F:20
## Mike : 5 Udall : 2 DFL: 2 1st Qu.:1997 1st Qu.:1944 M:80
## Mark : 4 Alexander: 1 I : 2 Median :2007 Median :1951
## Tom : 4 Ayotte : 1 R :46 Mean :2003 Mean :1951
## Bob : 3 Baldwin : 1 3rd Qu.:2011 3rd Qu.:1958
## Jeff : 3 Barrasso : 1 Max. :2013 Max. :1973
## (Other):76 (Other) :92
## Age YearsServed X X.1 X.2
## Min. :40.00 Min. : 0.0 Mode:logical Mode:logical Mode:logical
## 1st Qu.:55.00 1st Qu.: 2.0 NA's:100 NA's:100 NA's:100
## Median :62.00 Median : 6.0
## Mean :61.72 Mean : 9.9
## 3rd Qu.:69.00 3rd Qu.:16.0
## Max. :80.00 Max. :47.0
##
## X.3 X.4 X11
## Min. :11.00 Mode:logical Min. :11.00
## 1st Qu.:16.00 NA's:100 1st Qu.:16.00
## Median :19.50 Median :20.00
## Mean :22.09 Mean :22.42
## 3rd Qu.:28.00 3rd Qu.:28.00
## Max. :47.00 Max. :47.00
## NA's :66 NA's :67
#Using the table function
table(senate.2014$Affiliation)
##
## D DFL I R
## 50 2 2 46
table(senate.2014$FirstName)
##
## Al Amy Angus Barbara Ben Bernie Bill Bob
## 1 1 1 2 1 1 1 3
## Brian Carl Chris Chuck Claire Dan David Dean
## 1 1 2 2 1 1 1 1
## Deb Debbie Dianne Dick Ed Elizabeth Harry Heidi
## 1 1 1 1 1 1 1 1
## Jack Jay Jeanne Jeff Jeffrey Jerry Jim Joe
## 1 1 1 3 1 1 2 2
## John John Johnny Jon Kay Kelly Kirsten Lamar
## 5 1 1 1 1 1 1 1
## Lindsey Lisa Marco Maria Mark Martin Mary Max
## 1 1 1 1 4 1 1 1
## Mazie Michael Mike Mitch Orrin Pat Patrick Patty
## 1 1 5 1 1 2 1 1
## Rand Richard Rob Roger Ron Roy Saxby Sheldon
## 1 3 1 1 2 1 1 1
## Sherrod Susan Tammy Ted Thad Tim Tom
## 1 1 1 1 1 3 4
table(senate.2014$LastName)
##
## Alexander Ayotte Baldwin Barrasso Baucus Begich
## 1 1 1 1 1 1
## Bennet Blumenthal Blunt Boozman Boxer Brown
## 1 1 1 1 1 1
## Burr Cantwell Cardin Carper Casey Chambliss
## 1 1 1 1 1 1
## Chisea Coats Coburn Cochran Collins Coons
## 1 1 1 1 1 1
## Corker Cornyn Crapo Cruz Donnelly Durbin
## 1 1 1 1 1 1
## Enzi Feinstein Fischer Flake Franken Gillibrand
## 1 1 1 1 1 1
## Graham Grassley Hagan Harkin Hatch Heinrich
## 1 1 1 1 1 1
## Heitkamp Heller Hirono Hoeven Inhofe Isakson
## 1 1 1 1 1 1
## Johanns Johnson Kaine King Kirk Klobuchar
## 1 2 1 1 1 1
## Landrieu Leahy Lee Levin Manchin Markey
## 1 1 1 1 1 1
## McCain McCaskill McConnell Menendez Merkley Mikulski
## 1 1 1 1 1 1
## Moran Murkowski Murphy Murray Nelson Paul
## 1 1 1 1 1 1
## Portman Pryor Reed Reid Risch Roberts
## 1 1 1 1 1 1
## Rockefeller Rubio Sanders Schatz Schumer Scott
## 1 1 1 1 1 1
## Sessions Shaheen Shelby Stabenow Tester Thune
## 1 1 1 1 1 1
## Toomey Udall Vitter Warner Warren Whitehouse
## 1 2 1 1 1 1
## Wicker Wyden
## 1 1
table(senate.2014$AssumedOffice)
##
## 1966 1975 1977 1978 1979 1981 1985 1987 1992 1993 1994 1997 1999 2001 2002 2003
## 1 1 1 2 1 1 3 4 1 2 1 8 2 4 2 4
## 2005 2006 2007 2009 2010 2011 2012 2013
## 5 1 11 12 3 14 1 15
table(senate.2014$DOB)
##
## 1933 1934 1936 1937 1939 1940 1941 1942 1943 1944 1946 1947 1948 1949 1950 1951
## 2 4 3 2 2 3 2 2 4 4 4 4 2 3 7 4
## 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1968
## 5 2 3 8 2 2 2 1 3 3 3 3 1 2 1 1
## 1970 1971 1972 1973
## 1 3 1 1
table(senate.2014$Gender)
##
## F M
## 20 80
table(senate.2014$Age)
##
## 40 41 42 43 45 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
## 1 1 3 1 1 1 2 1 3 3 3 3 1 2 2 2 8 3 2 5 4 7 3 2 4 4
## 69 70 71 72 73 74 76 77 79 80
## 4 4 2 2 3 2 2 3 4 2
table(senate.2014$YearsServed)
##
## 0 1 2 3 4 6 7 8 10 11 12 14 16 19 20 21 26 28 32 34 35 36 38 47
## 15 1 14 3 12 11 1 5 4 2 4 2 8 1 2 1 4 3 1 1 2 1 1 1
table(senate.2014$Affiliation,senate.2014$Gender)
##
## F M
## D 15 35
## DFL 1 1
## I 0 2
## R 4 42
#Using the boxplot function
boxplot(senate.2014)
#Using the hist function
hist(senate.2014$Age)
hist(senate.2014$YearsServed)
hist(senate.2014$AssumedOffice)
# Those who have served more than 10 years:
# Using aggregate to compute the mean age of senators by party and gender:
print(aggregate (senate.2014$Age, list(PartyName=senate.2014$Affiliation), mean ))
## PartyName x
## 1 D 61.82000
## 2 DFL 57.50000
## 3 I 70.50000
## 4 R 61.41304
# Using aggregate to compute the mean age of senators by party and gender:
print(aggregate (senate.2014$Age, list(Gender=senate.2014$Gender), mean ))
## Gender x
## 1 F 60.900
## 2 M 61.925
#Sorting the data by seniority (years served)
SORTEDYEARS<-sort(senate.2014$YearsServed)
#Sorting the data by Age
SORTEDAGE<-sort(senate.2014$Age)
matplot(SORTEDYEARS,SORTEDAGE)
#Create a function DoLetters that takes a single integer argument, and returns the alphabet up to that number:
DoLetters<- function(n)
{
DoLetters<- LETTERS[1:n]
}
print(DoLetters(5))
## [1] "A" "B" "C" "D" "E"
#Create a function that takes two arguments that are integers between 1 and 26, and returns the sublist indicated by that range
DoLetters2 <-function(x,y)
{
DoLetters2<- LETTERS[x:y]
}