หนึ่ง Vector เก็บข้อมูลได้แค่ 1 ประเภท
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
1:10 # 1 2 3 4 5 6 7 8 9 10 16:25 #16 17 18 19 20 21 22 23 24 25 ## sequence generation seq(from = 1, to = 100, by = 5) # 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 ## help file help("seq") ## function c friends <- c("Daviid", "Marry", "Anna", "John", "William") ages <- c(20, 35, 43, 18, 27) is_mail <- c(TRUE, FALSE, FALSE, TRUE, TRUE) print(friends); print(ages); print(is_mail) # "Daviid" "Marry" "Anna" "John" "William" # 20 35 43 18 27 # TRUE FALSE FALSE TRUE TRUE |
…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## vector friends <- c("Nathan","Koy","Gle","King") length(friends) #เชคความยาว #เข้าไปดึงค่าใน obj นั้นขึ้นมา friends[1] # "Nathan" friends[3:5] # "Gle" "King" NA friends[c(1,3,4)] # "Nathan" "Gle" "King" #เปลี่ยนค่าใหม่ friends[1] <- "Ditto" friends # "Ditto" "Koy" "Gle" "King" friends[1:2] <- c("A","B") friends # "A" "B" "Gle" "King" ## หาตำแหน่ง index which(friends == "Gle") # 3 |
เก็บข้อมูลได้ 1 ประเภท แต่จะมี 2 dimension
คือ column
และ row
สร้าง Matrix ด้วยคำสั่ง matrix()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
m <- matrix(1:10, ncol = 2) m # [,1] [,2] # [1,] 1 6 # [2,] 2 7 # [3,] 3 8 # [4,] 4 9 # [5,] 5 10 m <- matrix(1:10, ncol = 2, byrow = TRUE) m # [,1] [,2] # [1,] 1 2 # [2,] 3 4 # [3,] 5 6 # [4,] 7 8 # [5,] 9 10 ## element wise computation m*2 # [,1] [,2] [,3] [,4] [,5] # [1,] 2 4 6 8 10 # [2,] 12 14 16 18 20 # [3,] 22 24 26 28 30 # [4,] 32 34 36 38 40 m <- matrix(c(1,2,3), ncol = 2) y <- matrix(c(4,5,6), ncol = 2) m*y # [,1] [,2] # [1,] 4 18 # [2,] 10 4 y <- 1:6 dim(y) <- c(3,2) y # [,1] [,2] # [1,] 1 4 # [2,] 2 5 # [3,] 3 6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
my_playlist <- list( fav_movie = "The Dark Knight", fav_song = "OMG", fav_artist = "NewJeans" ) my_playlist$fav_movie # "The Dark Knight" my_playlist[1] # $fav_movie # "The Dark Knight" my_playlist[[1]] # "The Dark Knight" my_playlist <- list( fav_movie = c("The Dark Knight","Marvel"), fav_song = c("OMG","XX"), fav_artist = "NewJeans" ) my_playlist$fav_movie[2] my_playlist[[1]][2] # "Marvel" ## customer database for our company customer_01 <- list( name = "Nathan", location = "BKK", age = 38, movies = c("John Wick", "Dark Knight") ) customre_02 <- list( name = "moh", lname = "wick", age = 42, movies = "john Wick 4", fav_weapon = "A Pencil" ) customer_db <- list( nathan = customer_01, moh = customre_02 ) customer_db$nathan$name # "Nathan" ## อยากรู้ key names(customer_db) # "nathan" "moh" |
คือ Table นั่นเอง ความรู้เรื่อง Data Frame เป็นพื้นฐานสำคัญของ Data Analists งานของเรามากกว่า 80% ทำงานกับ structured data เช่น ข้อมูลใน Excel/ Google Sheets และที่เราดึงออกมาจาก SQL databases
data.frame()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#แสดงชื่อ build-in data frame data() mtcars #build-in data frame ## create data frame from vector friends <- c("Nathan","Koy","gle","Kung","Noi") ages <- c(37,32,7,0,6) movie_lover <- c(T,F,T,F,T) df <- data.frame(id = 1:5, friend=friends, age=ages, movie =movie_lover) df # id friend age movie # 1 1 Nathan 37 TRUE # 2 2 Koy 32 FALSE # 3 3 gle 7 TRUE # 4 4 Kung 0 FALSE # 5 5 Noi 6 TRUE # alterative approach to create data frame in R customrers <- list( friends = c("John",NA), ages = c(23,45), movie = c(T,F) ) df <- data.frame(customrers) View(df) ## ตรวจสอบค่าว่าง is.na(df) ## หาจำนวนค่า NA sum(is.na(df)) ## วิธีดึงข้อมูล df$friends[2] df[[1]][2] ## ดึงทุก row , coloumn 2:3 df[ ,2:3] ##ดึง row 1:3, ทุก column df[1:3, ] ## df[, c(1,3)] df[ ,c("ages","friends")] ## lisa_index <- which(df$friends == "John") lisa_index df[lisa_index, "friends"] <- "Nathannnnnnn" ## ดึงข้อมูลตามเงื่อนไข df[df$age < 30, ] df[!df$movie, ] #ดึง row ที่ไม่ชอบดูหนัง #ดึงหลายเงื่อนไข and (&) or (|) condition <- !df$movie & df$ages > 30 #and df[condition, ] condition <- !df$movie | df$ages > 30 #or df[condition, ] #export เป็น csv write.csv(df, "friends.csv", row.names = FALSE) #read csv df <- read.csv("friends.csv") |
ดึงข้อมูลออกมาจาก Data structure Subset ด้วย Position, name, condition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
friends <- c("Daviid", "Marry", "Anna", "John", "William") ages <- c(20, 35, 43, 18, 27) ## by position friends[1] # "Daviid" friends[2] # "Marry" friends[1:4] # "Daviid" "Marry" "Anna" "John" friends[c(1,2,5)] # "Daviid" "Marry" "William" ## by condition ages[ages > 30] # 35 43 ages[ages <= 30] # 20 18 27 ## by name ใช้ function names() names(ages) <- friends ages # Daviid Marry Anna John William # 20 35 43 18 27 ages["Marry"] # Marry # 35 ages[c("Marry", "Anna", "John")] # Marry Anna John # 35 43 18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
## df[row, col] ## by position df[1, 3] # "Bangkok" df[1:2, 4] # TRUE TRUE df[1:2, 2:4] # ages location movie # 1 20 Bangkok TRUE # 2 35 Mahasarakham TRUE ## by name df[ ,"friends"] # "Daviid" "Marry" "Anna" "John" "William" df[, c("friends", "location")] # friends location # 1 Daviid Bangkok # 2 Marry Mahasarakham # 3 Anna Surin # 4 John Konkean # 5 William Loei ## by condition ให้ใส่เงื่อนไขใน row df[ df$friends=="Marry", ] # friends ages location movie # 2 Marry 35 Mahasarakham TRUE df [ df$movie==TRUE, c("friends", "ages", "movie")] # friends ages movie # 1 Daviid 20 TRUE # 2 Marry 35 TRUE # 5 William 27 TRUE df[ df$ages < 30, ] # friends ages location movie # 1 Daviid 20 Bangkok TRUE # 4 John 18 Konkean FALSE # 5 William 27 Loei TRUE |
ป้ายกำกับ:DataFrame, List, Matrix, R Data structure, Vector
Email : nathanbc46(at)gmail.com
Messenger : http://m.me/seenualpage
© SeeNual All rights reserved.
บทความ โดย สีนวลดอทคอม อนุญาตให้ใช้ได้ตาม สัญญาอนุญาตของครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า 4.0 International.