1) Data
types
:
Logical (true/false)
·
· Character
("string") "data"
·
Double (5 0) .
X = 5 0 .
·
Infinity (infl 2 = 5/0
2) Vectors :
Va C(10
,
20
,
30)
w
naming the vector
VIa second
elementS o the output is 20
3) Data frames :
Each row represents a case or observation Examples to find how
many
we use :
and each column represents a variable sum (names column = = 1)
simple code: data. frame (column 1 Column 2)
name ,
eghow many "action" movies
to see specific rows and columns we use: sum (data s action = = 1)
name[ ,
I which command
raclumn which (name I
budget == max (movies Sbudget ,narm-Truel)
um
7 [2 3]
egname[3 ,
name ,
ignore N A
.
↓
all colums ot row 3 row 2 column 3
,
To find a specific column name we use
,
$ Graphical and numerical summaries
name height (access the
heightcolumn) code : ( S
eg summary
we can do plots from the data frames Wit min 1st median , mean 3rd q , max
gives
·
, q , ,
1) For
histogram : his+ (x)
2) For boxplot : boxplot(x)
(namecolumn "blue")
eg hist ,
breaks-
-
"Sturges" -
col =
default , optional number
4)
Writing functions :
default arguments
-
square s function()E add function (a
1 b
,
= = a) [
return (x" 2) return (a +b)
3 3
square (5) add()
W
so"This wil I return 25
will return 3
, 5) Conditionals and loops :
·
If statements
x= 5
If (xx0)[
print ("positive")
3
·
If-else statements
If the if the condition is false it will allow an alternative condition.
X -
3
If (x > 0) [
print ("positive")
Yelsed
print ("negative")
3
·
If-else - if statements
multiple conditions to be checked
syntax
:
if (condition 1) [
#
code if cone 1 istrue
Jelseif (condition2)E
# code if condit 2 is true
.
JelseE
# code if
nothing above
is true
3
·
For loops
used to repeat a block of code for afixed number of iterations. example
total o
syntax
:
for (variable in sequence) [ for (J in-5 : -1) E
# code to execute in each iteration total <total + exp(j) /ja
3 3
print (total)