0% found this document useful (0 votes)
0 views

Programming Foundation

The document provides an overview of programming foundations, covering key concepts such as variables, data types, data structures, functions, and control flow. It includes examples of calculations, variable manipulation, and the creation of data frames in R. Additionally, it discusses the use of functions and control structures like if-else statements and loops.

Uploaded by

hunbyulna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
0 views

Programming Foundation

The document provides an overview of programming foundations, covering key concepts such as variables, data types, data structures, functions, and control flow. It includes examples of calculations, variable manipulation, and the creation of data frames in R. Additionally, it discusses the use of functions and control structures like if-else statements and loops.

Uploaded by

hunbyulna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
> Programming Foundation * variable + data type * data structure * function * control flow Calculation Last 15-2 15*2 10 17/2 35 15**2 # 545, 25 1 # comment 24 note to this code cell 3 print("hello world") [1] “hello world" 1142 > Variables income <- 35000 expense <- 25000 saving <+ income - expense auaune print (saving) [1] 10000 1 print (income) 2 print (expense) 3 print (saving) [2] 35e80 [2] 25e00 [1] 12280 14 create new variable 2 my_name <- "Beckhan” 3 print (ny_nane) [1] "Beckham" 1 # remove variable 2 rm(my_name) 1 print (my_name) Error in eval(expr, envir, enclos): object ‘my_nane’ not found Traceback: print(my_name) SEARCH STACK OVERFLOW 1 school <- "DataRockie” 2 print (school) [1] "DataRockie" 1# save income for 10 years, interest rate 5% 2 3 100@@ * (140.05) ** 20 265329,770814442 > Data Types © numeric character (text, string) logical (TRUE, FALSE) date factor (categorical - stats, special in R) 1 # numeric 2 gpa <- 3.41 3 print (gpa) [1] 3.42 Lage <- 35 2 print(age) 3 print (class(age)) [2] 35 [4] “numeric” 1 class(gpa) ‘numeric! 1 ## character 2 class(“hello world”) ‘character’ 1 ## concat two character 2 paste®(“hello", “world”) 1 paste("hello", "world", “hahaha") ‘hello world hahaha 1 # boolean => logical (TRUE, FALSE) Last == 2 TRUE 1 # not equal 25125 FALSE LIC == 2) FALSE 1# is R case sensitive? 2" FALSE 1 my_name = "toy" 2 my_Name = "top" Lmy_name == my_Name FALSE ## date today_date <- as.Date("2023-11-11") print (today_date) print (class(today_date)) Rune [1] "2023-11-11" [1] "Date" 1 as.character(today_date) "2023-11-11" 1 ## as.___() change data type 2 as. integer (FALSE) o 1 as.character (1234) "1234" 1 class(123) ‘numeric! 1 ## R name come from? 2 ## country of origin 3 4 ## Ross Thaka + Robert Gentlenan 5 6 ## vector => similar to Google Sheets Array 7 gender <- c("male*, “female”, “male") 8 9 gender <- factor(gender) 1 class(gender) ‘factor’ 1 ## factor has two categories 24## 1. nominal: male, female 3## 2. ordinal: low < med < high ~ Data Structures * vector (single data type) = matrix (single data type) * list * dataframe 1 #8 vector 2 scores <- ¢(88, 98, 5@, 65, 76) 1 ## vectorization (no need to write loop) 2 scores +2 90 + 92-52-6778 1 sum scores, average score 2 ## aggregate functions 3 print(sum(scores)) print (mean(scores)) print (min(scores)) print (max(scores)) print(length(scores)) ## count Nous [1] 369 [1] 73.8 [1] se [1] 9¢ f1] 5 aH subset scores <- ¢( John = 88. minnie = 9, david = 50, marry = 65, anna = 76) woevauaunn scores john: 88 minnie: 90 david: 50 marry: WH index starts at 1 ## by position 1,2,3 ## by name "david", "marry" 4 by condition aunune scores[scores > 80] john: 88 minnie: 90 1 ## update value in scores 2 scores[3] <- 68 3 scores john: 88 minnie: 90 david: 60 marry: 1 scores[S] <- 86 2 scores 65 anna: 65 anna; 76 76 john: 88 minnie: 90 david: «60 marry: 65 anna: 86 1 ## friends 5 persons 2 friends <- c("toy", "john", "mary", "david", “lisa") 1 paste("h » friends) ‘hi toy’-'hi john’ - hi mary’ «hi david’ -‘hi lisa’ 1 ## matrix => vector 2 dimensions 2ml <- mateix(1:100, nrow=10, byrow=TRUE) 3 ml * 2 # vectorization ‘Amatrix: 10 * 10 of type dbl 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 1 #8 matrix multiplication .dot 2m <- matrix(e(2,4,5,18), ncol=2) 3 4 m2 <- matrix(c(1,1,5,2), ncol=2) 5 6 maj m2 A matrix: 2x20f type dbl 26 1 # dot operation 2m x% m2 Amatrix: 2x20f type dbl 7 20 14 40 1 # dimension 2 dim(m1) 2-2 1## list , dataframe 2## list (key = value) 3 customer@1 <- list( 4 5 6 Bangkok", 7 favorite_films = c("Dark Knight", "The Marvels") 8 9 ) 10 customer@2 <- list( 11 nane = "jane", 12 age = 28, 13. city = "Seoul", 14 favorite films = c("About Tine", “Taylor Swift Concert") 45) 16 17 # nested lists 18 list_customers <- list(customer@1, customera2) 1 customer[["age"]] ## return value 35 1 customer["age"] ## return list Sage = 35 1 2 3 customer[["favorite_films"]][2] ‘The Marvels’ customer[[ "city" "Bangkok’ "]] cust_name = list_customers[[2]][["name"]] print (cust_name) [1] "Jane" # dataframe # the most important in data analyst role 4# create a new dataframe from scratch 5 6 df <- data. frame( 7 8 9 18 FER 1 2 3 id= 4: name = c("toy", “john age = c(28, 30, 31, 22, = (1, Ty Fy movie_lover ) # by condition # and |=or F, 1) anne"), df2 <- df[ (df$age >= 30) | (df$movie_lover) , ] 1H create city ( a new colunn ) 2 df$city <- c(rep("BKK",3), rep("LONDON",2)) # replicate 3 df id name 1 toy 2 john 3° may 4 jane 5 anne Adata frame: 5 «5 age movie_lover 28 30 3 22 25 BKK BKK BKK LONDON LONDON 1 df$random <- 100 2 3 ## remove column 4 df$random <- NULL 5 df Adata frame: 5 * 5 id name age_movie_lover — city 1 toy 28 TRUE BKK 2 john 30 TRUE BKK 3 may 31 FALSE BKK 4 jane 22 FALSE LONDON 5 anne 25 TRUE LONDON 1 ## write csv file 2write.csv(df, "movie.csv", row.names=FALSE) 1 ## read csv file 2movie <- read.csv("movie.csv") 3 movie Adata frame: 6 * 5 id name age movie_lover city 1 toy 28 TRUE BKK 2 john 30 TRUE BKK 3 mary = 31 FALSE BKK 4 jane 22 FALSE LONDON 5 anne 25 TRUE LONDON 1 #8 structure 2 str(movie) ‘data.frame': 5 obs. of 5 variables: $id int 12345 $ name chr "toy" "john" "mary’ $ age : int 28 38 31 22 25 $ movie_lover: logi TRUE TRUE FALSE FALSE TRUE $ city : chr "BKK" "BKK" "BKK" "LONDON jane" » Function 1 sum( 1:18 ) 55 1 #8 create new function 2 my_secret_formula <- function(start, end) { (start + end) * end / 2 } away ny_secret_formula(1, 10) 55 1 add_two_num <- function(x,y) x+y L result <- add_two_num(5,4) 2 result 1 greeting <- function() print(“hi!") 2 greeting() [a] "hit 1 greeting <- function(name = "John Wick") { 2 return( paste("Hi!", name) ) 3} 4 5 greeting() "Hil John Wick’ 1 greeting_city <- function(name="John", city="London") { 2 paste("Hi!", name, "Welcome to", city) 1 greeting_city(city="Bangkok", name="Mary") ‘Hil Mary Welcome to Bangkok’ 1 greeting city() ‘Hil John Welcome to London’ ¥ function take user input 1 usernane <+ readline("What is your name: " 2 password <- as.numeric(readline("Password: ")) hat is your name: toy Password: 123456 1 class(usernane) 2 class(password) ‘character’ ‘integer 1 greeting v2 <- function() { 2 ## readline get input in character 3 user_name = readline("Hi what's your name: ") 4 text = paste("Good morning!", user_name) 5 return(text) 6} 1 greeting v2() Hi what's your name: John Wick ‘Good morning! John Wick’ > Control Flow * if * forloop * while loop 1 ## ifelse 2## =IF() in google sheets 348 ifelse() in R 4 5 score <- 79 6 7 ifelse(score >= 80, "passed","failed") ‘failed’ 1 if-else 2 grading <- function(score) { 3 if ( score >= 86) { 4 return("A") 5} else if (score >= 70) { 6 return("B") 7} else if (score >= 68) { 8 return("C") 9 } else if (score >= 50) 10 return("D") 1} else ( 2 return("Failed") 3B OY 44} 1 ## function is re-usable 2 grading(57) 1 #8 for loop 2 #8 Tabsilludacldivinudtu Rk 3 ## vectorization 4 5 fruits <- c("banana", "apple", pineapple") 1 for (fruit in fruits) { 2 print(toupper(fruit) ) 3) [2] “BANANA” [1] "APPLE" [1] "PINEAPPLE* 1 toupper(fruits) "BANANA’ : ‘APPLE’ ‘PINEAPPLE’ 1 # while loop 2 count <- @ 3 4 while (count < 5) { 5 print(*hil") 6 count = count +4 7) [1] [1] [2] "hi! 1 apply(mtcars, 2, mean) 20.090628 cy! 6.1875 disp: 230.721875 hp: 146.6875 drat: 3,5965625 wt: 3.21725 qsec: 17.84875 vs: 0.4375 am: 0.40625 gear: 3.6875 carb: 2.8125 mpg: Summary Basic Programming 1. variable 2. data type: numeric, character, logical, date, factor 3. data structure: vector matrix list dataframe 4. function 5. control flow: if for while Homework > uhdequ > user davinainls

You might also like