ฮ้าย ฮายยยย วันนี้เรามารู้จัก ภาษา R กันดีกว่า

ภาษา R เป็นภาษาที่เหมาะกับ Data Science

เรามาเริ่มกันที่ Datatype ก่อนดีกว่า

Datatype มีกี่แบบ?

ที่ใช้กันบ่อยๆ จะมี 4 แบบ คือ

  • Numeric → (10.5, 55, 787) เป้นตัวเลข เป็นทศนิยมได้
  • Character → เป็น String
  • Logical → เป็น Boolean
  • Date

How To Assign value?

การจะ Assign value นั้น Easy มั๊กๆ ทำแบบนี้

a <- "test"

How To Convert Data Type?

Assign ว่าง่ายแล้ว เจอ Convert ซะหน่อย

เริ่มจาก Assign 100 เข้าไปใน x ซึ่งเป็น Numeric

x <- 100

แล้วเราจะ Convert เป็น String แบบนี้

char_x <- as.character(x)

เอ๊า แล้วมันมี Convert แบบไหนบ้างล่ะ

มี 3 ประเภท เท่านั้น!!

  • as.logical(…)
  • as.character(…)
  • as.numeric(…)
  • as.date(…)
  • as.factor(<vector>)

<aside> 📖

เราได้เรียนรู้ไปแล้ว ว่ามี Type ยังไงบ้าง มาต่อที่ Structure กันเลยดีกว่า

</aside>

Structure of R

1. Vector

Vectors จะเป็น List of item แล้ะที่สำคัญคือต้องเป้น Type เดียวกันด้วยนะ

ตย.

fruit <- c("banana","apple","orange")
	numbers <- c(1, 2, 3)
	numbers <- 1:10
	log_values <- c(TRUE, FALSE, TRUE, FALSE)

2. List

เป็นสิ่งที่จะบรรจุ data เข้าไป ไม่จำเป็นว่าต้องเป็น Type เดียวกัน และจะเรียงกันแล้วสลับตำแหน่งกันได้

my_list <- list(item1="a",item2=1,item3=true)
my_list$item3

3. Matrices

Matrix จะมี 2 Dimension นั่นก็คือ row และ column

#create metrix
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
#print
thismatrix[c(1,2),]
thismatrix[, c(1,2)]

4. Arrays

อันนี้จะคล้ายกับ Metrix แต่ว่าจะมีได้หลายๆ Dimension ได้(มากกว่า2)

# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
thisarray

# An array with more than one dimension
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray

5. Data Frames

เป็นข้อมูลที่จะโชว์ขึ้นมาเป็น Table

# Create a data frame
Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

# Print the data frame
Data_Frame

# result
  Training Pulse Duration
1 Strength   100       60
2  Stamina   150       30
3    Other   120       45

6. Factors

จะใช้สำหรับ การ Catagorize ข้อมูล

# Create a factor
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

# Print the factor
music_genre

# Result
[1] Jazz    Rock    Classic Classic Pop     Jazz    Rock    Jazz
Levels: Classic Jazz Pop Rock

Function

Function เป็น block of code ซึ่งจะ run ก็ต่อเมื่อถูกเรียก ซึ่งจะส่งผ่านข้อมูลเข้าไปเป้น parameter และยัง return ค่าออกมาได้

my_function <- function() {# create a function with the name my_function
  print("Hello World!")
}

my_function() # call the function named my_function

Arguments

argument คือค่าที่ส่งเข้าไปใน function

my_function <- function(fname) {
  paste(fname, "Griffin")
}

my_function("Peter")
my_function("Lois")
my_function("Stewie")

จาก Code

  • fname ใน function(fname) นั้นจะเป็น Parameter
  • Peter,Lois,Stewie จะเป็น Arguments

Control Flow

Control Flow นั้นจะใช้ในการควบคุมพฤติกรรมของโปรแกรมที่เราเขียน ซึ่งจะมี 3 แบบ

1. if-else

เราใช้ if-else ในการทำเงื่อนไข

x <- 100
if(x > 50){
  print("Awesome") 
} else {
  print("Try again") 
}

2. for loop

for loop จะใช้ในการทำ Action นั้นซ้ำของในแต่ละ Item ส่วนมาก Item นั้นมักจะอยู่ใน Vector

friends <- c("A", "B", "C")
for(name in friends){
  print(paste0("Hello ", name, "!"))
}

3. while loop

while loop จะเป็นการทำ Action นั้นซ้ำๆ จนกว่า Condition นั้นจะกลายเป็น False

x <- 10
while(x > 0){
  print(paste0("The value of x is ", x))
  x <- x - 1
}

Trending