#--------------------------------------------------------------- # A FIRST R SESSION: # -> OPEN # -> CLOSE # -> REOPEN #--------------------------------------------------------------- # We can start using R as a calculator 2+2 # The standard assignment operator in R is # " <- " or " = " # You can use both symbols indiscriminately. # R works using "objects" a a=2+2 a b=2*a # ... you have already noted that we use # to write comments # in the script file. Any word written after # will not be # executed a # print out a #--------------------------------------------------------------- # -> GETTING HELP #--------------------------------------------------------------- # Several resources are available to help you learn more about R. # These include several facilities WITHIN R itself and, of course, # on the Web. # The help() Function: # if you have problems with a particular function (e.g. sum), # you can get online help typying help(mean) # or ?mean # For special characters and some reserved words you must use "" # For example, if you have problems with "<" operator type help("<") # or ?"<" # For example, if you need help for the reserved # word "if" (basic control-flow constructs see later), type help("if") # or ?"if" # The example() Function # If you do not need all help information but only some example # use the example() function. example(mean) example("if") # The help.search() Function # The help() and example() functions help you when # you already know the name of the function you would like to use. # If you don't know what you are looking for, you can use # the help.search() function, a Google-style search through R documentation. # For instance, say you need a function to draw three-dimensional plots. Type help.search("3d plots") # or ??"3d plots" # R finds the packages that could contain functions for drawing 3d plots..... however it takes just forever to find them locally! use the web instead # Help for packages: # you can also learn about entire packages typing # help(package=PACKAGE's NAME) # For example, for the package "graphics" help(package=graphics) # Numerical operations help(Arithmetic) help(Logarithm) help.search("Logarithm") help(sqrt) help(Trig) help(Special) ### To get the list of all commands and datasets in a package just type #library(help=name of the package) library(help=graphics) #--------------------------------------------------------------- # CLOSE and SAVE our work in the working directory: # .Rdata file: it is the saved workspace, containing # all objects you created # .Rhistory file: it records your commands, to remind you # how that workspace was created. # What is the working directory and how to set it. # Get the working directory: getwd()###let you know where you are located on your hard disk # Check that your work space contains the objects # you created in the session ls() #it displays the names of the objects in the workspace #to change working directory one can use the Rconsole or the menu, using the R console requires to know the system path to the working directory, notice that R uses the Linux convetion to describe patterns: setwd() ##allows to set the working directory setwd("/Users/jona_air/Dropbox/anna fano/tutorial partitioning/IntroduzioneR") #--------------------------------------------------------------- # Packages: how to load packages and install packages #--------------------------------------------------------------- ### we can use Rconsole menu or use the line commands ################################################ ### the fuction install.packages allows package installation (not very easy to use) ### the function installed.packages() allows to very which packages are installed on the computer #### to make a package available in during the session we have to recall it and this can be done using the Console menu or the function library() library(entropart) ## this makes the package entropart available during the session ### Our sugestion is to install using the menu (remember the dependencies) and to recall packages using library #--------------------------------------------------------------- # R OBJECTS #--------------------------------------------------------------- # R variable types are called modes. # In the next sections we will see different R modes. # Let's start with simple R objects and their modes. # [1] MODE: NUMERIC (numeric {[integer, real] o complex}) #--------------------------------------------------------------- a=10 b=15.53 d=3.51+1.2i # check the mode of the objects mode(a) mode(b) mode(d) # [2] MODE: CHARACTERS #--------------------------------------------------------------- e="Hallo" f="World" mode(e) # [3] MODE: LOGICAL #--------------------------------------------------------------- g=TRUE h=FALSE i=F l=T mode(g) #--------------------------------------------------------------- #### VECTORS #--------------------------------------------------------------- # Vectors are created using the concatenating function "c". # Vector of numbers v1=c(5,2,3,1,9) v1 mode(v1) # Vector of characters v2=c("a","b","c") v2 mode(v2) # Vector of "objects" v3=c(a,b,d) v4=c(g,h,i,l) mode(v4) # empty vector v5=c() # Individual elements of a vector are accessed via ["position of the element"]. # For example if you want to select the second element of v1, v1[2] # Subsetting is a very important operation on vectors. # If you want to select a subset of consecutive elements # of the vector type ["position of the first element" : "position of the last element"] # For example, if you want to select from the second and the fourth element of v1 v1[2:4] v1 # If you want to select elements in non consecutive positions you type # [c("position 1","position 2","position 3")] # For example, if you want to select elements of position 3,2,5 of v1 v1[c(3,2,5)] # How many elements in your vector? length(v1) # You can also execute simple operations using your vectors sum(v1) mean(v1) prod(v1) cumsum(v1) v1-2 v1/2 # Simple operations with vectors: #--------------------------------------------------------------- # When applying an operation to two vectors that requires them to be of the # same length, R automatically recycles, or repeats, the shorter one, until it is # long enough to match the longer one c(2,4,1)+c(5,1,2,0,0,4) # that is equal to c(2,4,1,2,4,1)+c(5,1,2,0,0,4) v1*c(5,2) # that is equal to v1*c(5,2,5,2,5) # Vectors contain objects of the same mode. # If you concatenate elements of different modes ... new.1=c(v1,v2) mode(new.1) new.2=c(v1,v2,v3,g,h,v4) new.2 mode(new.2) # Change the mode of a vector as.character(v1) as.character(v3) as.character(v4) as.numeric(v4) as.numeric(v2) # .... what happens??? # [4] MODE: LIST #--------------------------------------------------------------- # One simple solution to put elements of different modes # together without changing their modes! # a list is a container for items of different data types. list.1=list(c("Hallo","Friends","Hallo"),c(30,50,0.5,0.1),c(TRUE,TRUE,FALSE)) list.1 mode(list.1) # How to access to list elements? # Solution 1: if the elements of the list do not have a name # as in list.1, just type the position using [[]] list.1[[1]] list.1[[1]][1] list.1[[1]][2] list.1[[1]][c(1,3)] list.1[[2]] list.1[[2]][4] list.1[[2]][1:3] list.1[[2]][c(1,4)] # Solution 2: if the elements of the list have a name, # you can directly access them using $ list.2=list(welcome=c("Hallo","Friends","Hallo"), mynumbers=c(30,50,0.5,0.1), myidea=c(TRUE,TRUE,FALSE)) list.2 list.2[[1]] # is equivalent to list.2$welcome list.2[[1]][c(1,3)] list.2$welcome[c(1,3)] ########################################################################## ###### Read data from a text (.csv) file ######################################################################## ### First of all verify how data are organized in the text file: is there a column header? which is the field separator (space, ",", ";")? which is the decimal delimiter (".", ",")? once these questions are answered we are ready to load data form a file present in the working directory dat=read.table("iris.txt",header=T,dec=".") dat1=read.table("iris.csv",header=T,sep=";") #### using ?read.table ## we can see all the options available to read text formatted data. ### to import data from databases (access, mysql etc.) there exist several packages such as RODBC (connect to most existing databases including excel), foreign (to read .dbf, SAS, SPSS, Matlab and more) str(dat) ### this function compactly display the internal structure of an R object while summary(dat) #### returns basic statistics from the columns of dat. summary is a generic function that applied to R objects returns summary information on the object containt depending on the object class ###R objects belong to classes and most generic functions have a specific behaviour for each class class(dat) ## a data frame is like an simple spreadsheet where different type of information may live together dim(dat) ## dat has 150 rows and 5 columns (equivalently nrow(dat) and ncol(dat)) #### all clumns in a dataframe must have the same number of rows, when a cell is empty it is assumed to be missing and denoted with "NA" dat2=read.table("irisNA.csv",header=T,sep=";") summary(dat2) #missing data influence results of statistical functions unless we specify what the function should do with the missing: mean(dat2[,1]) mean(dat2[,1],na.rm=T) ########################### Graphics plot(dat[,1],dat[,3],pch=20,xlab=colnames(dat)[1],ylab=colnames(dat)[3],col=dat[,5]) ### correlation round(cor(dat[,-5]),2) #### add the correlation to the plot text(4.5,6.2,paste("corr.",round(cor(dat[,1],dat[,3]),2))) ####Frequency distributions table(dat$Species) barplot(table(dat$Species),col=c("blue","orange","green")) pie(table(dat$Species)) ####histograms hist(dat$Sepal.Length) ##### boxplot boxplot(dat[,-5],col=rainbow(4)) #### or one variable boxplot(dat$Petal.Length~dat$Species,col=rainbow(3)) ########## a large part of the statistical procedure relie on the assumption that data are Gaussian. A simple graphical way to verify this assumption is trough quantile plots #where empirical quantiles are compared to the standard normal theoretical quantiles qqnorm(scale(dat[,1])) abline(c(0,1),col=2) ### if data lie on the red line (the quadrant bisecting line) the normality assumption is acceptable save.image("finalfirstsession.RData") # To remove all objects rm(list=ls()) ######## And eventually######################## # Few rules to be kept in mind in order to avoid chaos: ####################################### # 1. Keep your initial workspace empty – no objects # 2. Import the raw data and perform the analyses. Remember: a datafile must be as simple as possible, just column header, data organized so that each row is a case # 3. From the File menu, or using the line command save the workspace in a project folder with a name of your choice (but with an extension of .RData). # 4. Get into the habit of keeping good clean code in your script file and save that in the project folder as well. #5. By working keeping separate workspaces, all data objects and analysis objects will be available for subsequent analyses and there will be no need to import the data more than once.