name="DNW"
if (name == "DNW") {
="David"
fist="White"
lastelse if (name=="JLB") {
} ="Justin"
first="Benson"
lastelse {
} =NA
first=NA
last
}print(frsit)
x <- -3
if (x < 0) {
print("x is negative ")
} else if (x> 0) {
print ("x is positive")
} else if (x == 0) {
print ("x is positive")
} else {
print("x is invalid")
}
='a'
x
if (x == "a" || x=="A") {
=1
yelse if (x=="B" || x=="b") {
} =2
yelse {
} =3
y
}print(y)
Use '&' and '|' for bare logical statements
Use '&&' and '||' in conditional statements
(Latter uses short-circuit evaluation)
switch(x,
first = 1,
second = 2,
third = 3,
fourth = 4,
fifth = 5,
sixth = 6,
NA
)
print(10)
Sys.sleep(1)
print(9)
Sys.sleep(1)
print(8)
Sys.sleep(1)
print(7)
# ...
disp(10)
Sys.sleep(1)
for (val in 1:10) {
print(val)
Sys.sleep(1)
}
=1:10
xfor (val in x) {
print(val)
Sys.sleep(1)
}
=0;
last<- matrix(1,3,4)
my.matrix for (row in 1:nrow(my.matrix)){
for (col in 1:ncol(my.matrix)){
=3+last
my.matrix[row,col]=my.matrix[row,col]
last
}
}
print(my.matrix)
cat(my.matrix,sep="\n")
=10
xwhile (x > 0) {
print(x)
Sys.sleep(1)
<- x-1
x }
<-1
irepeat {
<-i+1
iSys.sleep(1)
print(i)
}
hit esc key to cancel execution
https://www.r-graph-gallery.com/
plot(1,1)
plot(c(1, 10), c(20, 30))
=1:10
x=20:30
yplot(x,y)
plot(x,y, type='l')
plot(x,y, type='l', col='red')
plot(x,y, type='l', col='red', cex=1.5, pch=1)
plot(x,y, type='l', col='red', cex=1.5, pch=2)
plot(x,y, type='l', col='red', cex=1.5, pch=2, lwd=2)
plot(x,y, type='l', col='red', cex=1.5, pch=2, lty=3)
plot(x,y,
type='l', col='red', cex=1.5, pch=2, lty=3,
main="My Plot", xlab="x values (au)", ylab="y values (au)")
#multiple
lines(c(1, 10), c(20, 30))
points(c(2.5, 3.5), c(3.5, 3.5))
#legend
=c("data1","data2")
my.labels=c("black","red")
my.colorslegend("topright", my.labels, fill=my.colors)
=c(15,50,35,40)
my.countspie(my.counts)
pie(my.counts, init.angle=45)
=c("test1","test2","test3")
my.labels=c("blue","red","black","violet")
my.colorspie(my.counts,
main="Test scores", label=my.labels, col=my.colors)
# legend
legend("topright", my.labels, fill=my.colors
x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)
barplot(y, names.arg = x)
col
density
width
horiz=True