install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec()
## Installing a package
install.packages("package-name")
## Removing a package
remove.packages("")
## Installed packages that need to be updated
old.packages()
## Updating old packages
update.packages()
## Using a function from a package without loading the package
### PackageName::function()
ggplot2::geom_point
print("Hello World!")
1 + 1
pi
sqrt(8)
()print
نیز برای چاپ کردن میتوانیم استفاده کنیم.
print(pi)
()print
را متمایز می کند این است که هر شی (Object) را که در R داریم در فرمت درست چاپ میکند.
print(matrix(c(1,2,3,4),2,2))
()print
در هربار تنها یک شی را چاپ میکند.
print("Pi number is equal to " , pi)
()cat
استفاده میکنیم.
cat("Pi number is equal to", pi , "\n")
()cat
توانایی چاپ بردار (Vector) های ساده را نیز دارد.
cat("12 months of the year are" , month.name , "\n")
->
استفاده کنیم.
s <- "this is a string"
x <- 4
s
print(x)
->
استفاده کنیم.
()ls
استفاده میکنیم.
()ls.str
استفاده کنید.
ls()
ls.str()
a <- 4
ls()
ls.str()
()rm
میتوانید متغیرها را از workspace خود پاک کنید.
print("Removing x")
rm("x")
ls.str()
##Clearing the workspace
rm(list = ls())
print("The workspace now :")
ls()
## Documentation of installed functions
?mean
## Documentation of functions
??highchart
## Documentation of packages or functions
help("")
#### Getting help on packages
vignette("")
()c
میتوانید یک بردار بسازید.
c(1,3,3*pi,4+5)
c(T,F,T)
()c
خود ، بردار باشند آنها را باز کرده و با دیگر مقادیر ترکیب میکند.
v1 <- c(1,4,5)
v2 <- c(8,5,6)
c(2,v1,v2,0)
v <- c(2,4,6)
u <- c(3,4,5)
u != v # 2 != 3 4 == 4 6 != 5
v > u # 2 < 3 4 == 4 6 > 5
u > 4
v*2 > 7
v <- c(0,10,20,30,7,11,23)
v[1]
v[-1]
v[2:4]
v[c(2,3,4)]
v[-c(2,3,4)]
v[v > 8]
v[v > median(v)]
4:9
-3:7
6:4
()seq
استفاده کنید.
seq(from = 3, to = 8, by = 2)
seq(from = 5, to = 20, length.out = 4)
seq(1,2,length.out = 5)
()rep
استفاده میکنیم.
rep(2,times = 4)
c(1:5,seq(6,10),(1:5)+10)
u <- c(10:1)
u
print("Mean :")
mean(u)
print("Standard Deviation :")
sd(u)
print("Variance :")
var(u)
print("Min :")
min(u)
print("Max :")
max(u)
print("Median :")
median(u)
## For a random sample of normal distribution
sample <- rnorm(n = 5, mean = 0, sd = 1)
sample
##For other distributions:
## Use runif for uniform sample
## Use rbinom for binomial sample
## ...
()dnorm
چگالی توزیع را و ()pnorm
تابع توزیع را بر میگردانند.
vec <- 1:6
vec
newVec <- c(vec,10,vec)
newVec
## Inserting Data into a Vector
append(vec,8:10, after = 2)
1:6 + 1:3
## 1 2 3 4 5 6
## +
## 1 2 3 1 2 3
c(1,2,3,10,20,30) + 1:5
## 1 2 3 10 20 30
## +
## 1 2 3 4 5 1
## Create a matrix using matrix() function
# 2*4 matrix
matrix(1:8, nrow = 2)
#2*4 matrix, each row is filled first
matrix(1:8,nrow = 2, byrow = T)
# 4*2 matrix
matrix(1:8, ncol = 2)
## Name columns and rows of a matrix
m <- matrix(1:4,nrow = 2)
colnames(m) <- c("col one","col two")
rownames(m) <- c("row one","row two")
m
## Calling elements
print(m[2,])
print(m[,2])
print(m[2,2])
# Arithmatic on matrices
print("Scalar Multiplication:")
m * 2
print("Element Wise Multiplication:")
m * m
print("Matrix Multiplication:")
m %*% m
## Create a dataframe with data.frame()
df <- data.frame(
ID = 1:5,
Name = c("Ali","Reza","Kiarash","Nima","Hossein"),
Grade = c(20,16,17.5,14,19))
df
## Summary of a dataframe
summary(df)
##Selecting elements
df$Grade
df[1,]
df[,1]
df[df$Grade > 18,]
df[c("ID","Grade")]
## Depending on the format of dataset
## for example inorder to read a .csv file, use read.csv()
read.csv("Address of the file")