Salta ai contenuti. | Salta alla navigazione

Strumenti personali

lab 1_ introduzione a R

Plain Text icon Lab 1_introduction.txt — Plain Text, 3 kB (3404 bytes)

Contenuto del file

#INTRODUCTION TO R
#R is a powerful language and environment for statistical computing and graphics
#It is a public domain project

#GETTING STARTED: R INSTALLATION
#To install R on your computer for free, go to the home website of R
#https://www.r-project.org/
#GO To "download R": remember that you should choose your preferred CRAN mirror 
#We'll youse R and not R-studio (as we saw during lecture, the last one has a different layout. 

#SOME STARTING RULES: 
#R makes use of the�#�sign to add comments, so that you and others can understand what the R code is about. 
#Just like Twitter! Comments are not run as R code, so they will not influence your result. 
#For example : 

#calculate 1+3
#is a comment


#R AS A CALCULATOR
#R can be used as a calculator. You can just type
#your equation in the command window after the�>�:

10+36 #ADDITION. R will give the answer after "[1]"
#you'll obtain [1]46
10-3 #SUBTRACTION

9/3 #DIVISION

10*3 #MULTIPLICATION

3^2 #The caret symbol performs EXPONENTIATION

#----TO DO----#

#Using the console write the following comment: INTRODUCTION TO THE SOFTWARE
#Calculate  3+6 and insert a simple comment explaining the result
#Calculate 3*3 and insert a comment explaining the result
#Calculate 3/3 and insert a comment explaining the result
#Calculate 6-3 and insert a comment explaining the result
#Calculate (3+3)*2 and insert a comment explaining the result
#Calculate 3^2 and insert a comment explaining the result

#--------#

#2 OPERATIONS separated from semicolon  �;�

3+5*(3.5/15)+5-(2/6*4); 3+2 

#Operation using square root values: 

10+(7-2)*4-8/2+sqrt(9)

#VARIABLE CREATION
#Assign a value to a variable (you may use = or direct arrow <-)
x=6 # R registers the assignment, equal to x<-6

#when we recall x and tape return key, we'll visualize the content of our variable
x 

#CLASSIFICATION: DIFFERENT TYPES OF VARIABLES
#R classify the variables as INTEGER, COMPLEX, LOGICAL and CHARACTER

#numeric#
class(7.3)
class(3)

#integer#
k = as.integer(5)
k
class(k)
is.integer(k)

x=5.7
k=as.integer(x)
k

#complex and logical#
# COMMANDS: == equal; >= greater or equal; <= lower or equal; >greater; < lower; != different

z= 3>7
z
class(z)

u=TRUE;v=FALSE
u&v    #u and v  which results F #

#character#
name="valentina"
class(name)
x=as.character(14.25) #we difine as character variable  �14.25�,  so when we ask R will identify �14.25� as character

x  
class(x)

#visualization of variables#
surname = "Mini" 
name= "Valentina"
paste(name,surname) #and observe the result



#VECTORS 
#Create a vector v composed by 5 elements 1,2,3,4,5
v=c(1,2,3,4,5)
v
length(v)#number of elements composing vector v
str(v) #structure of vector v
min(v) #minimum value in v
max(v) #maximum value in v

#------TO DO---#
# execute the following vectors operations and  insert  for each a comment about the result
s=sum(v)
s

p=prod(v)
p

cs=cumsum(v)
cs

cp=cumprod(v)
cp

#----------------#

# Note: when you look for a command in R, simply write: "help(command)" OR "?command"
# Note2: if you copy and paste the scrip you'll can see all the results
# Note 3 : remember 2 basic rules = 
#1) R is case sensitive discriminates between uppercase and lowercase letters
#2) R replace a variable when is created using the same letter