Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Websites
– https://www.rstudio.com/products/rstudio/dow
nload/
(download and install Desktop version)
R language: Overview
• Open source and open development.
• Design and deployment of portable, extensible,
and scalable software.
• Variety of statistical and numerical methods.
• High quality visualization and graphics tools.
• Effective, extensible user interface.
• Supports the creation, testing, and distribution of
software and data modules: packages.
R as a Calculator
> log2(32)
[1] 5
> print(sqrt(2))
[1] 1.414214
> pi
[1] 3.141593
1.0
0.5
sin(seq(0, 2 * pi, length = 100))
0.0
-0.5
-1.0
0 20 40 60 80 100
Index
Variables
> a <- 49
numeric
> sqrt(a)
[1] 7
Example:
> list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23)
#Accessing the third element.
print(list_data[3])
#giving a name to each field and access using the name
> doe <- list(name="john",age=28,married=F)
> doe$name
[1] "john“
> doe$age
[1] 28
Matrices
# Print the 1st row and 3rd column of the 1st matrix.
print(result[1,3,1])
[1] 13
> result[1,3,1]=5
> print(result[1,3,1])
[1] 5
Data Frames
data frame:
rectangular table with rows and columns; data within
each column has the same type (e.g. number, text, logical),
but different columns may have different types.
Represents the typical data table that researchers come
up with – like a spreadsheet.
Example:
> # Create the data frame.
# Extract 3rd and 5th row with 2nd and 4th column.
result <- empdata[ c(3,5), c(2,4)]
print(result)
Data Frames
Expand Data Frame
A data frame can be expanded by adding columns and rows.
Add Column
Just add the column vector using a new column name.
After rbind
Subsetting
Subsetting
Individual elements of a vector, matrix, array or data frame are
accessed with “[ ]” by specifying their index, or their name
> a
localization tumorsize progress
XX348 proximal 6.3 0
XX234 distal 8.0 1
XX987 proximal 10.0 0
> a[3, 2]
[1] 10
> a["XX987",]
localization tumorsize progress
XX987 proximal 10 0
>a
Example: XX348
localization tumorsize progress
proximal 6.3 0
XX234 distal 8.0 1
XX987 proximal 10.0 0
> a[-c(1,2),]
localization tumorsize progress
XX987 proximal 10.0 0
> a[c(T,F,T),]
subset rows by a localization tumorsize progress
logical vector XX348 proximal 6.3 0
XX987 proximal 10.0 0
if (logical expression) {
statements
}
else {
alternative statements
}
Switch syntax:
switch(expression, case1, case2, case3....)
Loops
When the same or similar tasks need to be
performed multiple times; for all elements of a
list; for all columns of an array; etc.
for(i in 1:10) {
print(i*i)
}
i<-1
while(i<=10) {
print(i*i)
i<-i+sqrt(i)
}
mysquare(6)
functions
# Create a function to calculate (a * b + c).
• Examples:
• a <- 'Start and end with single quote'
• b <- "Start and end with double quotes"
• c <- "single quote ' in between double quotes"
• d <- 'Double quotes " in between single quote'
String Manipulation
Concatenating Strings - paste() function
> print(paste("How","are","you?"))
[1] "How are you?“