# R

Using R in statistical approach
ชนิดข้ อมูลและตัวแปร
(Data Types and Variables)
Mongkon Sophon
Walailak University: Computer Engineering

Data Types and Variables

345 45 76 (Types of Data)

>A 123456

G (Logic) > A True False
• <=5< J /45L= (Character/Text) > A A Dog
• <=>74 (Numeric)

2

Data Types and Variables

6 OO45 45 76
• Vector : c()
• Matrix : matrix()
• Data Frame : data.frame()
• List : list()
• Array : array()

3

Data Types and Variables

4

Vector (>= > 5 )R
• Vector Lื5 Array 1

T ื5 Row Matrix (> UR =)
• c(45 76 V T A WX1, 45 76 V T A WX2, Z, 45 67 V T A XW n)
WX 1 ( =)
...
1

2

3

45 67 V T A WX

4

n

Data Types and Variables

Vector (>= > 5 )R

9

4

1

2

1

0

3

45 67 V T A WX

4

5

6

Data Types and Variables

<=

(Variables)

• ืX5 <=

= 45 67
• ืX5 <=
<- 45 67
• 45 67 -> ืX5 <=
> Wa =A > b
Object

X

9

4

1

2

1

0

3

45 67 V T A WX

4

7

Data Types and Variables

<=

(Variables)
X +2

+2

+2

+2

+2

9

4

1

0

9+2
=11

4+2
=6

1+2
=3

0+2
=2

8

Data Types and Variables

<=

(Variables)
X
9

4

1

0

X[1
X[1]

X[2
X[2]

X[3
X[3]

X[4
X[4]

** NA T a ึ l A W45 67 m V T A <n o **

Data Types and Variables

List (7
(7 R)
• List Lื5

V >5 Vector T7 ao <= A5 < > b 5< T ึX 5<

>3Wa= <
• list(vector1, vector2, Z, vectorn)

9

Data Types and Variables

Matrix (>(>

UR)

• Matrix Lื5 Array 2
WX 1 ( =)

XW 2
(L57< R)

11

12

13

14

21

22

23

24

31

32

33

34

41

42

43

44

10

Data Types and Variables

Matrix (>(>

11

UR)

• Matrix(vector 45 67, nrow=pV =

=, ncol=pV = L57< R,

byrow=FALSE T ื5 TRUE)
• byrow T a ึ 45 67 WX A>4 l >
UR <n > Wa
A T ALA TRUE l A A T ALA FALSE

**45 67

Matrix 5 > b

= AT ื5l A

3>3Wa= < **

Data Types and Variables

12

Data Frame ( 5O45 76 )
• Data Frame > b

q q < GT=A Matrix 7G Array 7A =Lื5
Data Frame pG W7< JmG> b Array 4 3 2
A
A45 76 A
3< 7
5O45 67l3
• data.frame(vector1, vector2, Z, vectorn)

13

Data Types and Variables

Array
• Array Lื5 7rA 45 45 67 WX 6 OA p<3> Os l=> b A= aA5ao

V T A A o UึX W
V T 3 l3

7< JmGL7 a Matrix A V T <O45 67 3 Array pG
=A 2
• array(45 67, c(4 3 XW1, 4 3 WX2, 4 3 WX 3, ..., 4 3

WXn))

Data Types and Variables

<=5aA Array 3
WX 3
(L= 7ึ )
WX 1 ( =)

XW 2
(L57< R)

14

Data Types and Variables

<=5aA Array 3
L= 7ึ G3<O <n WX 1
L= 7ึ G3<O <n WX 2
L= 7ึ G3<O <n WX 3
L= 7ึ G3<O <n WX 4

15

16

Data Types and Variables

OOtu T<3
• 1.) p

45 67>= >
7V 3<O 7 <=
• 2.) V 45 67 <n T 3
TheNumber2
• 3.) p > sO45 67 A5l

5 R > sOLA A5l Wn 34, 24, 35, 105, 71, 0.5, 14, 22, 11, 0.99
ืX5 TheNumber
TheNumber V L6m 2 p <n > sOq77<vwRl= <=
Wn 6 45 >

UR

25

30

14

15

35

10

14

18

7

55

10

57

22

35

54

42

Data Types and Variables

17

OOtu T<3
• 4.) p

Data Frame > sO45 67 A5l Wn p
l3q73< 5A l Wn

V =p=<a rA pV = 5 L

Name

Age

Sex

Smoking

Jason Scott

24

Male

TRUE

Patrick Loy

21

Male

FALSE

Harry Josh

23

Male

TRUE

Kyle Grey

22

Female

FALSE

Male

FALSE

Neo Steward 21