1+1
## [1] 2

R is just letting you know that this line begins with the first value in your result R will not return anything that follows Hashtag #, We used it for comments

2*3
## [1] 6
2+5
## [1] 7
2*3
## [1] 6
5 / 6*20
## [1] 16.66667
6 / (4 - 1)
## [1] 2
100:130
##  [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
## [20] 119 120 121 122 123 124 125 126 127 128 129 130

The colon operator (:) returns every integer between two integers. It is an easy way to create a sequence of numbers. If you type incomplete commandsR will display a + prompt, which means R is waiting for you to type the rest of your command.

Either finish the command or hit Escape to start over: If you type a command that R doesn’t recognize, R will return an error message. In that case,R is just telling you that your computer couldn’t understand or do what you asked it to do

6 / (4 - 1)
## [1] 2

Until now, you have just done some basic mathemathic and nothig was saved. you save data by storing it inside an R object. You can create new objects with the assignment operator <- keyboard shortcut for <- : Alt + - (the minus sign) # Objects

my_number <-1378
x<-5
A <-35 
A
## [1] 35
A/7
## [1] 5
t<-1+2
ey<-t/2
ey
## [1] 1.5
test<-10:100
length(test)
## [1] 91

Do not use the name with special symbol such as: ^, !, $, @, +, -, /, or *: Objects moddes are numerical, integeral,chacter and logical, ech object have mode and length

weight_g <- 55
weightt_kg <- weight_g/1000
weightt_kg
## [1] 0.055

Vectors

Integer_number<- c (15, 23, 8, 42, 4, 16)

numeric_number<- c (3.14, 0.0002, 6.022E23)

character<- c ("Hello", "Nice", "Great")

logical<- c(TRUE, FALSE, TRUE)
a<-1+2
a
## [1] 3
a*3
## [1] 9
mode(a)
## [1] "numeric"
length(a)
## [1] 1
f<- "Hello"
mode(f)
## [1] "character"
length(f)
## [1] 1
compar<-TRUE
mode(compar)
## [1] "logical"
length(compar)
## [1] 1

logical

x <- c(1,8,3)
y <- c(4,5,6)
x>y
## [1] FALSE  TRUE FALSE
y>x
## [1]  TRUE FALSE  TRUE
class(x)
## [1] "numeric"

Functions

In the following, the function is “round” which rounds a numeric value x to the specific numbers based on digit nuumbers you specified

A <- round(3.6567, digits=2)

Estamating the mean of the sequence number of 1 to 6

mean(1:6)
## [1] 3.5
mymean <- mean(1:6)
mymean
## [1] 3.5

Each function in R has three basic parts: a name, a body of code, and a set of arguments. To make your own function, you need to replicate these parts and store them in an R object, You can do this with the function “function”. To do this, call function() and follow it with a pair of braces, {}

name <- function(arguments) {
  body
}

Example-1

add_three <- function(x){
  y <- x + 3
  return(y)
}
add_three(5)
## [1] 8

Example-2-This function has two parameters

add_multi <- function(x, y){
  r <- x + 3*5-y
  return(r)
}
add_multi(6, 8)
## [1] 13

Matrices

Matrices store values in a two-dimensional array, just like a matrix from linear algebra

die<- 1:10
die
##  [1]  1  2  3  4  5  6  7  8  9 10
dim(die) <- c(2, 5)
h <- matrix(die, nrow = 2, ncol = 5)

subset of a matrix format is [rows, columns]

h[1, ]  # first row
## [1] 1 3 5 7 9
h[ ,4]  #4th column
## [1] 7 8

Array

While matrices are confined to two dimensions, arrays can be of any number of dimension Create an array

a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
## , , 1
## 
##      [,1]     [,2]     [,3]    
## [1,] "green"  "yellow" "green" 
## [2,] "yellow" "green"  "yellow"
## [3,] "green"  "yellow" "green" 
## 
## , , 2
## 
##      [,1]     [,2]     [,3]    
## [1,] "yellow" "green"  "yellow"
## [2,] "green"  "yellow" "green" 
## [3,] "yellow" "green"  "yellow"

List

list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)
## [[1]]
## [1] "Red"
## 
## [[2]]
## [1] "Green"
## 
## [[3]]
## [1] 21 32 11
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] 51.23
## 
## [[6]]
## [1] 119.1

Factors

Factors are the data objects which are used to categorize the data and store it as levels

food <- factor(c("low", "high", "medium", "high", "low", "medium", "high"))
levels(food)
## [1] "high"   "low"    "medium"

##Data Frames Like matrices, but columns of different modes Organized list, where components are columns of equal length rows

Cities<-c("Shiraz","Yazd","Babolsar","Ardebil", "Zahedan")
Geography<-c("south","center","north","northwest","east")
Province<-c("Fars","Yazd","Mazandaran","Ardebi", "Sistan")
Sea<-c(FALSE,FALSE,TRUE,FALSE, FALSE)
Rainfall<-c(120,80,300,250,60)

mywork<-data.frame(Cities, Geography, Province, Sea, Rainfall)
mywork
##     Cities Geography   Province   Sea Rainfall
## 1   Shiraz     south       Fars FALSE      120
## 2     Yazd    center       Yazd FALSE       80
## 3 Babolsar     north Mazandaran  TRUE      300
## 4  Ardebil northwest     Ardebi FALSE      250
## 5  Zahedan      east     Sistan FALSE       60
mywork$Province
## [1] "Fars"       "Yazd"       "Mazandaran" "Ardebi"     "Sistan"
mywork$Geography
## [1] "south"     "center"    "north"     "northwest" "east"
mywork$Cities[3]
## [1] "Babolsar"
barplot.default(height = Rainfall, names.arg = Cities, col= "blue", xlab = "Cities", ylab = "Rains") 

Data import

The easiest way to import data is using .csv format. you can save your spreadsheet in excel as .csv, comma delimated

setwd("D:/R workshop_Modares")
test<-read.csv("final_flower.csv", header = TRUE)
head(test)
##           Genus size
## 1 Dracocephalum   10
## 2 Dracocephalum    8
## 3 Dracocephalum   12
## 4 Dracocephalum   15
## 5 Dracocephalum   16
## 6 Dracocephalum   20

#Installing Packages

install.packages("ggplot2", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Saeid/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## 
##   There is a binary version available but the source version is later:
##         binary source needs_compilation
## ggplot2  3.5.1  4.0.1             FALSE
## installing the source package 'ggplot2'
install.packages("maps", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Saeid/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## 
##   There is a binary version available but the source version is later:
##       binary source needs_compilation
## maps 3.4.2.1  3.4.3              TRUE
## installing the source package 'maps'
library(ggplot2)
library(maps)

###To have the latest development of package, it is better using devtools,The devtools package contains functions that help with R package development #### install.packages(“devtools” , repos =“http://cran.us.r-project.org”) library(devtools) library(usethis) install_github(“liamrevell/phytools”)

#Plot data

library(maps)
library(ggplot2)
ggplot() + 
   geom_point(test, mapping =  aes(x = Genus, y = size))

ggplot() + 
   geom_boxplot(test, mapping =  aes(x = Genus, y = size), col = c("red", "blue","green","black"))