####
#
# 02A Selecting parts of objects
#
####

#
# Object analysis: useful functions
#

class() # explore elements’ data class
length() # explore number of first dim. of object
dim() # explore dimensions of two-dimensional obj.
nrow() # number of rows
ncol() # number of columns

head() # first few rows of data 
tail() # last few rows of data 
str() # explore structure of object

names() # names in the named vector - one dimension 
rownames() # names of rows - two dimensions 
colnames() # names of columns - two dimensions

#
# Selecting: one dimension
#

peaches <- c("first","second","third","fourth") 

peaches[3]

#
# Selecting: two dimensions
#

cars <- c("BMW","Audi","VW")
type <- c("3","A4","Passat") 
price <- c(1200000,1164000,950500)
consumption <- c(6.2,5.9,5.9)

df <- data.frame(cars,type,price,consumption, stringsAsFactors = F) 

#
# Selecting: two dimensions, single row by index
#

df[2, ]

#
# Selecting: two dimensions, single column by index
# 

df[ ,1]

#
# Selecting: two dimensions, single row and column by index
#

df[2,1]

#
# Selecting: two dimensions, column by name
#

df[,"cars"]

df[ ,"cars",drop=FALSE ]

df$cars

df$price

class(df$price)

#
# Selecting: multiple rows/columns
#

df[, c(1,3)]

df[, 1:2]

df[1:2,c(1,3)]

#
# Practice 1
#

eurodist

#
# Practice 2
#

# install library poliscidata and load dataset world