intr_R_assignment

TASK-1 : Video Review : {gt} Table Battles || Eurovision || RStudio ( Posit PBCV) asdasddasd

  • Jesse and Rich are having a little competition to create the Eurovision Song Contest Winners album. They will both take turns creating a table in R using the data containing the Competition Winners.

  • https://liamdbailey.com/posts/2020-11-27-making-beautiful-tables-with-gt/ From there they received the data for free.

  • Jesse said that he wanted to use the flags and codes of the country and that they were all ready on the Liam D. Bailey site and that he spent 3 or 4 hours trying to do this. This part was a bit challenging. (It is necessary to have the files ready, then use the text conversion and then other image functions.)

  • She found the title by googling it with a beautiful font called “Rock Salt”. (I will use this too.)

  • Since he wanted to have a table that responds to different inputs, he customized the table by adding “chose a country” and “chose a rank form 1 to 3” options with some coding. For example, when Germany is selected, Germany data appears. It is also possible to select multiple countries at the same time.

  • Rich created an animated painting by adding gifs he took from YouTube next to the songs. At the same time, the background of the “Eurovision” text in the title has a structure that slides and changes color.

  • I would never have thought that such things could be done with R. A truly impressive work has emerged.

TASK-2 : Differences between R and Python

1- Syntax Difference

Assignment Method
my_name <- "Aykut Simsek"
print(my_name)
[1] "Aykut Simsek"
myName = "Aykut Simsek"
print(myName)
Aykut Simsek

2- Vectors and Lists

One of the frequently used tools for storing data in Python is list. However, in the R language, vector are used to store data.

# We can use c() function for creating a vector.
my_courses <- c("Data Anlytics", "Quality Management", "Material Science", "Probability and Statistics", "Production Planning and Control", "MUH101")
print(my_courses)
[1] "Data Anlytics"                   "Quality Management"             
[3] "Material Science"                "Probability and Statistics"     
[5] "Production Planning and Control" "MUH101"                         
# We can use [] for creating a list.
myCourses = ["Data Anlytics", "Quality Management", "Material Science", "Probability and Statistics", "Production Planning and Control", "MUH101"]
print(myCourses)
['Data Anlytics', 'Quality Management', 'Material Science', 'Probability and Statistics', 'Production Planning and Control', 'MUH101']

3- Indexing

my_favorite_teacher <- "Erdi Dasdemir"
# It is a real information
my_favorite_teacher[0]   # -> character(0), did not give us "E"
character(0)
my_numbers <- c(1, 10, 100, 1000)
my_numbers[1]            # -> first element
[1] 1
my_numbers[1:3]          # -> 1 and 3 both included
[1]   1  10 100
myFavoriteTeacher = "Erdi Dasdemir"
# It is a real information
myFavoriteTeacher[0]      # -> gives us "E"
'E'
myNumbers = (1, 10, 100, 1000)
myNumbers[0]             # -> first element
1
myNumbers[1]             # -> second element (not first)
10
myNumbers[1:3]           # -> 1 included and 3 excluded
(10, 100)

TASK-3 : NA Example

library("dslabs")
Warning: package 'dslabs' was built under R version 4.3.2
data(na_example)
print(na_example)
   [1]  2  1  3  2  1  3  1  4  3  2  2 NA  2  2  1  4 NA  1  1  2  1  2  2  1
  [25]  2  5 NA  2  2  3  1  2  4  1  1  1  4  5  2  3  4  1  2  4  1  1  2  1
  [49]  5 NA NA NA  1  1  5  1  3  1 NA  4  4  7  3  2 NA NA  1 NA  4  1  2  2
  [73]  3  2  1  2  2  4  3  4  2  3  1  3  2  1  1  1  3  1 NA  3  1  2  2  1
  [97]  2  2  1  1  4  1  1  2  3  3  2  2  3  3  3  4  1  1  1  2 NA  4  3  4
 [121]  3  1  2  1 NA NA NA NA  1  5  1  2  1  3  5  3  2  2 NA NA NA NA  3  5
 [145]  3  1  1  4  2  4  3  3 NA  2  3  2  6 NA  1  1  2  2  1  3  1  1  5 NA
 [169] NA  2  4 NA  2  5  1  4  3  3 NA  4  3  1  4  1  1  3  1  1 NA NA  3  5
 [193]  2  2  2  3  1  2  2  3  2  1 NA  2 NA  1 NA NA  2  1  1 NA  3 NA  1  2
 [217]  2  1  3  2  2  1  1  2  3  1  1  1  4  3  4  2  2  1  4  1 NA  5  1  4
 [241] NA  3 NA NA  1  1  5  2  3  3  2  4 NA  3  2  5 NA  2  3  4  6  2  2  2
 [265] NA  2 NA  2 NA  3  3  2  2  4  3  1  4  2 NA  2  4 NA  6  2  3  1 NA  2
 [289]  2 NA  1  1  3  2  3  3  1 NA  1  4  2  1  1  3  2  1  2  3  1 NA  2  3
 [313]  3  2  1  2  3  5  5  1  2  3  3  1 NA NA  1  2  4 NA  2  1  1  1  3  2
 [337]  1  1  3  4 NA  1  2  1  1  3  3 NA  1  1  3  5  3  2  3  4  1  4  3  1
 [361] NA  2  1  2  2  1  2  2  6  1  2  4  5 NA  3  4  2  1  1  4  2  1  1  1
 [385]  1  2  1  4  4  1  3 NA  3  3 NA  2 NA  1  2  1  1  4  2  1  4  4 NA  1
 [409]  2 NA  3  2  2  2  1  4  3  6  1  2  3  1  3  2  2  2  1  1  3  2  1  1
 [433]  1  3  2  2 NA  4  4  4  1  1 NA  4  3 NA  1  3  1  3  2  4  2  2  2  3
 [457]  2  1  4  3 NA  1  4  3  1  3  2 NA  3 NA  1  3  1  4  1  1  1  2  4  3
 [481]  1  2  2  2  3  2  3  1  1 NA  3  2  1  1  2 NA  2  2  2  3  3  1  1  2
 [505] NA  1  2  1  1  3  3  1  3  1  1  1  1  1  2  5  1  1  2  2  1  1 NA  1
 [529]  4  1  2  4  1  3  2 NA  1  1 NA  2  1  1  4  2  3  3  1  5  3  1  1  2
 [553] NA  1  1  3  1  3  2  4 NA  2  3  2  1  2  1  1  1  2  2  3  1  5  2 NA
 [577]  2 NA  3  2  2  2  1  5  3  2  3  1 NA  3  1  2  2  2  1  2  2  4 NA  6
 [601]  1  2 NA  1  1  2  2  3 NA  3  2  3  3  4  2 NA  2 NA  4 NA  1  1  2  2
 [625]  3  1  1  1  3 NA  2  5 NA  7  1 NA  4  3  3  1 NA  1  1  1  1  3  2  4
 [649]  2  2  3 NA NA  1  4  3  2  2  2  3  2  4  2  2  4 NA NA NA  6  3  3  1
 [673]  4  4  2  1 NA  1  6 NA  3  3  2  1  1  6 NA  1  5  1 NA  2  6  2 NA  4
 [697]  1  3  1  2 NA  1  1  3  1  2  4  2  1  3  2  4  3  2  2  1  1  5  6  4
 [721]  2  2  2  2  4 NA  1  2  2  2  2  4  5 NA NA NA  4  3  3  3  2  4  2  4
 [745] NA NA NA NA  2  1 NA  2  4  3  2 NA  2  3  1  3  4 NA  1  2  1  2 NA  3
 [769]  1  2  1  2  1  2  1  2  2  2  2  1  1  3  3  1  3  4  3 NA NA  4  2  3
 [793]  2  1  3  2  4  2  2  3  1  2  4  3  3  4 NA  1  4  2  1  1  1  3  1  5
 [817]  2  2  4  2 NA  1  3  1  2 NA  1  2  1  2  1 NA  1  3  2  3  2 NA  2  1
 [841]  4  2 NA NA NA  2  4  2 NA NA  3  1 NA  5  5  2  2  2 NA  2  1  3  1  3
 [865]  2  4  2  4 NA  4  1  2  3  2  3  3  2  3  2  2  2  1  3  2  4  2 NA  3
 [889]  3  2  2 NA NA  3  2  1  2  4  1  1  1  1  4  3  2 NA  3  2 NA  1 NA  3
 [913]  2  1  1  1  2 NA  2  2  3  3  2 NA NA  4  5  2  2  2  1  2  3  1  3  3
 [937]  4  3 NA  1  1  1 NA  4  3  5  1  1  2 NA  2  2  2  2  5  2  2  3  1  2
 [961]  3 NA  1  2 NA NA  2 NA  3  1  1  2  5  3  5  1  1  4 NA  2  1  3  1  1
 [985]  2  4  3  3  3 NA  1  1  2  2  1  1  2  2 NA  2
number_of_na <- sum(is.na(na_example))
print(number_of_na)
[1] 145
cat("Bu vektorde toplam", number_of_na, "adet NA bulunmaktadır")
Bu vektorde toplam 145 adet NA bulunmaktadır
  • Python’da yaptığımız gibi print içerisinde string ve değişkeni aynı anda R’da ekrana yazdıramadım.

  • ChatGPT’ye “R dilinde tek parantez içinde hem bir değişken hem bir character türünü ekrana yazdırabileceğimiz bir fonksiyon bulunuyor mu?” diye sordum.

  • *degisken <- 42 character <- "Merhaba, Dünya!"

    Değişken ve karakter türünü tek parantez içinde ekrana verdirme: cat("Değişken: ", degisken, " Karakter: ", character, "\n")*

  • Bu çıktıyı aldım ve R’da bu işlev için cat() fonksiyonunu kullanmamız gerektiğini öğrendim.

library("dslabs")
data(na_example)

naless_example <- ifelse(is.na(na_example), 0, na_example)

print(naless_example)
   [1] 2 1 3 2 1 3 1 4 3 2 2 0 2 2 1 4 0 1 1 2 1 2 2 1 2 5 0 2 2 3 1 2 4 1 1 1 4
  [38] 5 2 3 4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
  • NA’lar yerine “0” ekleyemediğim için ChatGPT’den yardım aldım.

  • # NA’ları 0 ile doldur na_example_filled <- ifelse(is.na(na_example), 0, na_example)

  • Bu kod, is.na fonksiyonuyla na_example veri çerçevesindeki NA değerlerini kontrol eder ve ifelse ile NA değerlerini 0 ile değiştirir. Ardından, na_example_filled adlı yeni bir veri çerçevesi veya vektör oluşturulur ve NA değerleri 0 ile değiştirilmiş şekilde görüntülenir.

Back to top