########################################################################### # PUBLG100: Introduction to Quantitative Methods # # Week 1 Solutions: Introduction to Quantitative Analysis # # ## ------------------------------------------------------------------------ rm( list = ls() ) ## ------------------------------------------------------------------------ getwd() ## ----eval=FALSE---------------------------------------------------------- ## setwd("N:/PUBLG100") ## ------------------------------------------------------------------------ # assign the 5 elements to a a <- c( 2, 4, 6, 8, 10) # check what a contains a ## ------------------------------------------------------------------------ a <- a[- length(a)] a ## ------------------------------------------------------------------------ # create a again a <- c( 2, 4, 6, 8, 10,4) # delete last element a <- head(a, -1) # see what a contains now a ## ------------------------------------------------------------------------ # assign the values to the matrix m <- matrix( c( 3, 6, 9, 2, 3, 5, 54, 48, 42), 3, 3, byrow = T) # print the contents of m to the screen (check what m contains) m ## ------------------------------------------------------------------------ # assing the values to the matrix m <- rbind( c(3, 6, 9), c(2, 3, 5), c(54, 48, 42)) # check what m contains m ## ------------------------------------------------------------------------ # assign a 1 to row 1 and column 1 of m m[1, 1] <- 1 # assign a 1 to row 2 and column 2 of m m[2, 2] <- 1 # assign a 1 to row 3 and column 3 of m m[3, 3] <- 1 # check what m contains m ## ------------------------------------------------------------------------ # assigning the values from Exercise 7 to m again m <- rbind( c(3, 6, 9), c(2, 3, 5), c(54, 48, 42)) # assigning a 1 to each element on the diagonal of m diag(m) <- 1 # checking what m contains m ## ------------------------------------------------------------------------ # assign the csv file to df df <- read.csv("http://uclspp.github.io/PUBLG100/data/polity.csv") ## ------------------------------------------------------------------------ # assign(overwrite) df to df but only those rows where # the variable year is equal to 1991 df <- df[ df$year == 1991, ] # look at the first 6 rows of data set df head(df) ## ------------------------------------------------------------------------ # summary() returns summary statistics # the $ indicates that you want to access something in data set df # in this case the variable democ summary( df$democ) # table() produces a frequency table table(df$democ) ## ------------------------------------------------------------------------ # assign (overwrite in this case) df with df but only those rows (observations) # of df where democracy is greater or equal to zero df <- df[ df$democ >= 0, ] ## ------------------------------------------------------------------------ hist( df$democ)