I. Ozkan, PhD 
 Professor 
 MIS 
 Cankaya
University
 
iozkan@cankaya.edu.tr
  
Fall 2025
Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread.
– The tidyverse style guide
🚩 camelCase (Javascript)
🚩 PascalCase (Python)
✅ snake_case
-, and
_# Good
fit_models.R
utility_functions.R
# Bad
fit models.R
foo.r
stuff.r
00_download.R
01_explore.R
...
09_model.R
10_visualize.R
Pay attention to capitalization
Should be Human readable:
# good
report-draft-notes.txt
# bad
temp.r
# good
fig-eda.png
fig-model-3.png
# bad
figure eda.PNG
fig model three.png
In R script files or in Rmd files try to comment lines of codes frequently. Files should also be break up into readable chunks by using
Pay attention to internal structure
# Load data ---------------------------
...
# Summarize data ----------------------
... 
# Plot data ---------------------------
...
# Save data ---------------------------
_ to make
object name easier to read. Try to use minimum number of words# Good
day_one
day_1
# Bad
DayOne
dayone
first_day_of_the_month
djm1
# Bad
T <- FALSE
c <- 10
mean <- function(x) sum(x)
Note: T is reserved for TRUE and c is for c() and mean is a function name
# Good
x[, 1]
# Bad
x[,1]
x[ ,1]
x[ , 1]
Parentheses
# Good
mean(x, na.rm = TRUE)
# Bad
mean (x, na.rm = TRUE)
mean( x, na.rm = TRUE )
Note: Careful with
operators (==, +, -,
<-, etc.). Try to use parentheses such that operators
are surrounded by spaces
# Good
height <- (feet * 12) + inches
mean(x, na.rm = TRUE)
# Bad
height<-feet*12+inches
mean(x, na.rm=TRUE)
() when used with
if, for, or while (We will see
these constructs later)# Good
if (debug) {
  show(x)
}
# Bad
if(debug){
  show(x)
}
# Good
if (debug) {
  show(x)
}
# Bad
if(debug){
  show(x)
}
Read the rest of the Tidyverse Style Guide as we cover R scripting/Programming
R has five basic or “atomic” classes of objects:
character (“a”, “1”, “TRUE” - with quotation marks)
numeric (real numbers, 1.234, 18, 1e25)
integer (1, 2, 100)
complex (1 - 2i, 3 + 5i)
logical (True/False, T, F, TRUE, FALSE)
The most basic type of R object is a vector
A vector can only contain objects of the same class
Before moving to creating different data types and using/manipulating them, let’s discuss some basic built-in functions we will use frequently
c(): Combine Values into a Vector or List
## [1] 1 2 3 4
## [1] 1 2 3 4
## [1] TRUE
## NULL
## NULL
## [1]  7 -4  2  0
## [1] TRUE
## [1] 4
# before beginning (just in case)
# print() function, see ?print, Print Values
# examples  
print("Hello World") # no object specification ## [1] "Hello World"
## [1] 3
## [1] 1 2 3 4 5
## [1] 1
## [1] 1.234568
## [1] 1.2346
## [1] 1.23457
sep = string(s)
to each element and then outputs them.## Hello World
## 3
## 3 Hello World
## 3-Hello World
## 2-3-Hello World
## 2-3-Hello World
## [1] 1 2 3 4 5
## 1-2-3-4-5-Hello World
## [1] "1  One"
## [1] "1 One"
## [1] "1, One"
## [1] "1  are numbers" "2  are numbers" "3  are numbers" "4  are numbers"
## [5] "5  are numbers"
## [1] "1" "2" "3" "4" "5"
## [1] "1, 2, 3, 4, 5"
## [1] "1, 2, 3, 4, 5  are numbers"
## [1] "1" "2" "3" "4" "5"
## [1] "1,2,3,4,5"
## [1] "1 What" "2 What" "3 What" "4 What" "5 What"
## [1] "1 and 2 and 3 and 4 and 5"
Objects can be explicitly coerced from one class to another using
the as.XXXX functions, if available. Some basic functions
are:
is.XXX() functions are also available
## [1] "numeric"
## [1] FALSE
## [1] TRUE
## [1] "6"
## [1] TRUE
## [1] 6+0i
## [1] TRUE
## [1] NA
rep() function, see ?rep: Replicates the
values## [1] 1 1 1 1 1
##  [1] 1 2 1 2 1 2 1 2 1 2
## [1] "a" "b" "c" "d" "a" "b" "c" "d"
##  [1] "a" "a" "b" "b" "c" "c" "d" "d" "a" "a" "b" "b" "c" "c" "d" "d"
## [1] "a" "a" "b" "b" "c" "c"
## [1] "abc" "abc" "abc"
## [1] "abc" "abc" "abc"
seq() function, see ?seq: sequence
generation## [1]  0.0  2.5  5.0  7.5 10.0
## [1]  0  2  4  6  8 10
# for dates (we will cover dates in a bit later, but for now)
## first days of years
seq(as.Date("1999/1/1"), as.Date("2008/1/1"), "years")##  [1] "1999-01-01" "2000-01-01" "2001-01-01" "2002-01-01" "2003-01-01"
##  [6] "2004-01-01" "2005-01-01" "2006-01-01" "2007-01-01" "2008-01-01"
##  [1] "2023-01-01" "2023-02-01" "2023-03-01" "2023-04-01" "2023-05-01"
##  [6] "2023-06-01" "2023-07-01" "2023-08-01" "2023-09-01" "2023-10-01"
## [11] "2023-11-01" "2023-12-01"
##  [1] "2020-01-01" "2020-04-01" "2020-07-01" "2020-10-01" "2021-01-01"
##  [6] "2021-04-01" "2021-07-01" "2021-10-01" "2022-01-01" "2022-04-01"
## [11] "2022-07-01" "2022-10-01" "2023-01-01"
sample() function: takes a sample of the specified
size from the elements with or without replacement
set.seed() is a utility function that helps to
generate same random numbers
sample(x, size, replace = FALSE, prob = NULL)
##  [1]  1  5  8  2  4  7  9  6 10  3
## [1] 1 7
## [1] 5 9
## [1] 9 4
## [1] 7 1
## [1] 9 4
# 100 random samples with associated probabilities (STAT course is useful)
x <- sample(c("a","b"), prob=c(0.8,0.2), size=100, replace=TRUE)
table(x) # table function  ## x
##  a  b 
## 83 17
# 1000 random samples with associated probabilities
x <- sample(c("a","b"), prob=c(0.8,0.2), size=1000, replace=TRUE)
table(x) # table function  ## x
##   a   b 
## 798 202
# 1000 random samples with associated probabilities
# Relative weights (0.5/0.6, 0.1/0.6)
x <- sample(c("a","b"), prob=c(0.5,0.1), size=1000, replace=TRUE)
table(x) # table function  ## x
##   a   b 
## 837 163