# # Practice 1 # # load the dataset eurodist ed <- eurodist # explore the dataset class(ed) str(ed) # convert the dist object to matrix ed.matrix <- as.matrix(ed) # extract distances from Stockholm stock <- ed.matrix[,"Stockholm"] # get min and max - version 1 - does not show names max(stock) min(stock) # get min and max - version 2 - sort the vector - see the whole vector visually sort(stock) # get min and max - version 3 - sort the vector ascending/descending and get the first element stock.asc <- sort(stock) stock.asc[1] stock.asc[2] stock.desc <- sort(stock,decreasing = T) stock.desc[1] # get min and max - version 4 - use specific function which(stock == max(stock)) which(stock == min(stock)) # cologne - lisbon distance ed.matrix["Cologne","Lisbon"] # # Practice 2 # # Install and load the package “poliscidata” install.packages("poliscidata") library("poliscidata") # Create a new object containing the dataset “world” world.df <- world # Explore variables in the dataset (use help() function) help(world) #oops, not very well documented dataset str(world) # long structure colnames(world) # better to just show the names of variables # Extract countries into separate vector country.names <- world.df[,"country"] country.names <- world.df$country # Extract country IDs into separate data frame country.ids <- world.df[,"natcode",drop = FALSE] # Find the Czech Republic in the data frame country.names # so far, we only know how to do it manually = finding the number in the vector of country names country.names[41] # Extract the appropriate row from the data frame world.df[41,] # Find all V4 countries (CZ, SK, HU, PL) country.names country.names[c(41, 68, 124 ,135)] # Extract them into a separate data set world.df[c(41, 68, 124 ,135),] v4.df <- world.df[c(41, 68, 124 ,135),] # Extract only freedom indicators for these countries (all column names starting with “free_”) colnames(world.df) # again, we can only do it manually now. Best idea is to check column names of the data frame and find appropriate indexes of the columns of interest indicators <- colnames(world.df) # generate a vector of the column names - column names have same positions as columns in the data frame = if we find right positions of names, we get correct column positions indicators[50] indicators[51] indicators[62] indicators[63] indicators[51:62] # check if the columns are correct # option 1 - get it from the original data frame called world.df world.df[c(41, 68, 124 ,135),51:62] # option 2 - get it from the V4 subset we created in the previous step v4.df[,51:62]