####
#
# 02 Objects in R
#
####

#
# Object types: vector
#

c(2,3,5) 

vec <- c("aa", "bb", "cc", "dd", "ee")

vec

#
# Object types: matrix
#

mat <- matrix(data = c(1,2,3,4,5,6,7,8,9,10,11,12), 
            nrow = 3, 
            ncol = 4)

mat 

#
# Object types: data frame
#

cars <- c("BMW","Audi","VW")

type <- c("3","A4","Passat") 

price <- c(1200000,1164000,950500)

consumption <- c(6.2,5.9,5.9)

mydf <- data.frame(cars,type,price,consumption) 

mydf

#
# Object types: list
#

numbers <- c(2, 3, 5) 

strings <- c("aa", "bb", "cc", "dd", "ee") 

mylist <- list(numbers, strings, 3)

mylist 


#
# Libraries/packages
#

install.packages("sna")

library("sna")

library(sna)

library("sna")

detach("package:sna", unload = TRUE)

#
# Object analysis
#

length(1:30)

dim(mydf)

length(mydf)

nrow(mydf)

ncol(mydf)

class(mydf)

str(mydf)

describe(mydf)

install.packages("psych")

library("psych")

describe(mydf)

head(mydf) 

tail(mydf)

colnames(mydf)

View(mydf)

fix(mydf)

#
# 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

#
# Practice 1
# 

#Create a data frame with the following structure

#name   age   sex     econ_scale  soc_scale
#Jose	  17	  male	  -3	        -7.2
#Sara	  22	  female	0.6	        0.2
#Maria	21	  female	2	          0
#Frank	21	  male	  -3	        0.5
#John	  18	  male	  3.1	        3

#
# Practice 2
#

# Install library “psych”
# Enable the library
# Summarize and explore the data frame created in the Practice 1
# Find and use all available functions (creative task)

#
# Practice 3
#

# Type eurodist in your script
# Create an object from the eurodist dataset
# Visually inspect the data source
# Figure out the type of the data source (creative task)

#
# Practice 4
#

# Create a matrix with following parameters
# 6 by 6 layout
# Contains numbers 0 and 1 in a 0 1 0 1 0 1 0 1 pattern (creative task)
# The pattern is organized by rows

#       [,1]	[,2]	[,3]	[,4]	[,5]	[,6]
# [1,]	0		  1		  0		  1		  0		  1
# [2,]	0		  1		  0		  1		  0		  1
# [3,]	0		  1		  0		  1		  0		  1
# [4,]	0		  1		  0		  1		  0		  1
# [5,]	0		  1		  0		  1		  0		  1
# [6,]	0		  1		  0		  1		  0		  1