第一回 基礎ゼミ
Data aggregation and Visualization

2018.4.25
Fukuda Lab.
Natsuho Ihoroi

Outline・Introduction ~before starting “R”
・Basic calculation
・Data Visualization
・If文 and for文
・Package１

Advantages of programming
It is possible
・to analysis enomous amount of data・to carry out troublesome processing efficiently

What is R
Statistical analysis software for statiscal processing

Merit
The library is substantial

Demerit
Processing of“for statement” is slow
2

How to open R files
Method 1

Method 2

Click the file directly

Serch “R” on the home screen, andopen file from the upper left button

3

The R screen

3. Files and variables
1. To write programming

4. Graph output and packages
2. Calculation result4Setting the Working Space
Method 1
Copy and type by yourself(change: “＼” → “/”)

Method 2
Session→set working directory→to source file location
5

How to execute
1. write the script/code
2. Select the range you want to execute
3. Ctrl + Enter

Simple Calculation

Addition+

Subtraction-Multiplication*

Division/6

Data structure
VectorVectorMatrixMatrixArray

Data frame

1次元，長さを持つベクトル

2次元配列

C( , , , )rbind( a, b ) : Row bind
cbind( a, b ) : Column bind
Matrix AMatrix BMatrix( 1:8, nrow=2, ncol=4)
data Number ofrows

Number ofcolmins

Access to matrix data

X[ 1, 2 ]row column

7Data structure
Array

行列の多次元拡張

Array( 1:24, dim=c(2,4,3)
dimension

Useful function for vector, matrix and array
apply function
Apply( x, 1, mean )
Data

1:row
Function
2:column (mean, sum,,,)C(1,2):element8

Data structure

Imagesrbind

Matrix1234+4212345678xm2[1,2]456781357
2468

2

xm2[2, ]Array
3 Xa[ , , 1] 429

Read the csv file
Data frame

The data is basically “csv file”“xlsx file” cannot be readRead.csv( “file name” )Movie sales data

You can checkthe data byclicking here

10Line graphplot(data)

X axis

Y axis11

Bar graphbarplot(data)

Y axis12

Pie chartpie(data)

13

If文

Setting “a”” If a>0, “b=a+100
if (条件式：conditional expression){
実行すること: what to do} else if (条件式2: conditional expression 2 ){実行すること.
.
｝else {実行すること｝

Otherwise “b=-100”

Output “b”

14Define therepeat countfor文

RepeatWhat to do

Finishmaxfor (i in 1:100){

Repeat county[i] <- max(x[I, ] )}What to do

1

21

41

61

81

2

22

42

62

82

3

23

43

63

83

:

:

:

:

:

18

38

58

78

98

19

39

59

79

99

20

40

60

80

100
15

Application
Evaluate movies

Create matrix

Create empty matrix
to input evaluation value

Read csv fileand bind

Bind withoriginal movie datafor 文

If 文

Repeat × the number of row
Sales> 50
Great !
Sales<50 ,audience> 300
Good !
Audience<300
Normaloutput

16

Application

Evaluation value was added

Write csv filewrite.csv( data, “file name” )17How to install package
Method 1

1. Click “Package” in the bottom right
2. Click “install” and type “packages name”

Method 2“install.packages(“package name”)
18How to use package

library ( “package name” )ggplot2
A graphic drawing package
in which beautiful graphs can be drawn

dplyr
Package specialized for data frame
It can process at high spped because written in C++
19

dplyrOkinawa.csv

Siteinfo.csv

filterNarrow doan the rowsthat matched theconditional expression

Filtering 3 sites“Churaumi”,”Syuri-castle and“Kokusai-dori”

20dplyrarrangegroup_bysummrise

SortfunctionGrouping
function

Aggregation
function

Orderbydate

left_join

2017/8/6

2017/8/7

21

ggplot

If you change here, you candraw other graphs

g<-ggplot()+ geom_bar
visualize
Plot(g)saveggsave(file=“file name”, plot=p)22