# HAFTA 3
(sayisal_vektor <- c(1,2,3))
## [1] 1 2 3
tamsayi <- 2L
typeof(tamsayi)
## [1] "integer"
tamsayi
## [1] 2
ondaliksayi <- 2.5
typeof(ondaliksayi)
## [1] "double"
ondaliksayi
## [1] 2.5
(karakter_vektor <- c("a","b","c")) ## cift tirnak
## [1] "a" "b" "c"
(mantiksal_vektor <- c(TRUE,TRUE,FALSE))
## [1] TRUE TRUE FALSE
a <- 1
length(a)
## [1] 1
x <- c(3,4,5)
y <- c(1,2,3)
length(x)==length(y)
## [1] TRUE
x+y
## [1] 4 6 8
x-y
## [1] 2 2 2
x<-1:9
y<-c(1,2,3)
length(x)/length(y)
## [1] 3
x+y
## [1] 2 4 6 5 7 9 8 10 12
x-y
## [1] 0 0 0 3 3 3 6 6 6
x/y
## [1] 1.0 1.0 1.0 4.0 2.5 2.0 7.0 4.0 3.0
x <- 1:5
y <- c(1,2)
# vektör eleman sayıları farklı olduğunda
length(x)/length(y)
## [1] 2.5
x<- 1:10
sum(x)
## [1] 55
prod(x)
## [1] 3628800
sort(x)
## [1] 1 2 3 4 5 6 7 8 9 10
sort(x,decreasing = T)
## [1] 10 9 8 7 6 5 4 3 2 1
rev(x)
## [1] 10 9 8 7 6 5 4 3 2 1
sd(x)
## [1] 3.02765
max(x)
## [1] 10
min(x)
## [1] 1
which.max(x)
## [1] 10
which.min(x)
## [1] 1
ad <- c("Ali","Elif","Su","Deniz","Aras",
"Berk","Can","Ece","Efe","Arda")
ad[1]
## [1] "Ali"
ad[c(1,4,6)]
## [1] "Ali" "Deniz" "Berk"
ad[10]
## [1] "Arda"
ad[length(ad)]
## [1] "Arda"
ad_1<-ad[-1]
(ad_2 <- ad[-c(1,4,6)])
## [1] "Elif" "Su" "Aras" "Can" "Ece" "Efe" "Arda"
ad[11] <- "Esma"
ad
## [1] "Ali" "Elif" "Su" "Deniz" "Aras" "Berk" "Can" "Ece" "Efe"
## [10] "Arda" "Esma"
ad[c(12,13)] <- c("Demet","Samet")
ad
## [1] "Ali" "Elif" "Su" "Deniz" "Aras" "Berk" "Can" "Ece" "Efe"
## [10] "Arda" "Esma" "Demet" "Samet"
ad <- append(ad,"Asli",4)
ad
## [1] "Ali" "Elif" "Su" "Deniz" "Asli" "Aras" "Berk" "Can" "Ece"
## [10] "Efe" "Arda" "Esma" "Demet" "Samet"
ad <- c("Ali","Elif","Su","Deniz","Aras",
"Berk","Can","Ece","Efe","Arda")
boy <- c(160,165,170,155,167,162,169,158,160,164)
kilo <-c(50,55,57,50,48,65,58,62,45,47)
names(boy)
## NULL
names(boy) <- ad
names(boy)
## [1] "Ali" "Elif" "Su" "Deniz" "Aras" "Berk" "Can" "Ece" "Efe"
## [10] "Arda"
boy
## Ali Elif Su Deniz Aras Berk Can Ece Efe Arda
## 160 165 170 155 167 162 169 158 160 164
boy["Ali"]
## Ali
## 160
?seq
## starting httpd help server ... done
# seq ile bir sekans oluşturma. seq(from,to,by)
seq(from=1,to=10,by=1)
## [1] 1 2 3 4 5 6 7 8 9 10
seq(1,3,length.out=5)
## [1] 1.0 1.5 2.0 2.5 3.0
rep(c(3,4,5),3)
## [1] 3 4 5 3 4 5 3 4 5
?rep
rep(c(3,5,7),each=3)
## [1] 3 3 3 5 5 5 7 7 7
rep(c(3,5,7),each=3,times=3)
## [1] 3 3 3 5 5 5 7 7 7 3 3 3 5 5 5 7 7 7 3 3 3 5 5 5 7 7 7
rep(c(1,2,3,4),each=2)
## [1] 1 1 2 2 3 3 4 4
rep(1:3,1:3)
## [1] 1 2 2 3 3 3
paste(1:4)
## [1] "1" "2" "3" "4"
class(paste(1:4))
## [1] "character"
paste("test", 1:10)
## [1] "test 1" "test 2" "test 3" "test 4" "test 5" "test 6" "test 7"
## [8] "test 8" "test 9" "test 10"
paste("test", 1:10, sep="_") #sep ile istediğin karakterle ayırabilirsin
## [1] "test_1" "test_2" "test_3" "test_4" "test_5" "test_6" "test_7"
## [8] "test_8" "test_9" "test_10"
paste("test",1:10,"puan",sep="_")
## [1] "test_1_puan" "test_2_puan" "test_3_puan" "test_4_puan" "test_5_puan"
## [6] "test_6_puan" "test_7_puan" "test_8_puan" "test_9_puan" "test_10_puan"
paste("test",1:10,round(rnorm(10),3),sep="_")
## [1] "test_1_-0.705" "test_2_-1.517" "test_3_1.479" "test_4_0.67"
## [5] "test_5_-0.635" "test_6_-0.647" "test_7_-0.57" "test_8_-0.055"
## [9] "test_9_0.603" "test_10_-0.392"
paste("test",c("A","B","C","D",1:4))
## [1] "test A" "test B" "test C" "test D" "test 1" "test 2" "test 3" "test 4"
#Random Veri Üretimi
sample(0:100,5) #Rastgele Örnekleme
## [1] 86 42 9 29 11
runif(100,0,5) #uniform dağılıma göre üret
## [1] 3.94789014 1.92638160 2.25858767 3.70052636 4.27941978 1.91125141
## [7] 0.81331211 0.16214176 0.38221481 0.83599518 1.21272288 2.83967099
## [13] 4.57066820 3.01715596 3.24002335 0.75746767 0.02330671 1.50302249
## [19] 3.41088057 1.41077477 4.56214981 3.16348532 2.60000807 0.49924752
## [25] 3.00628457 0.52418381 2.97048008 4.52384357 2.83650955 1.38374565
## [31] 1.29689308 3.50418624 2.66578290 1.15599290 4.15585885 3.97090810
## [37] 2.18194760 2.40661197 0.50002656 2.69608358 2.26729688 4.91634511
## [43] 1.66563676 1.19787260 1.87542795 0.13399941 2.07663264 1.99411331
## [49] 3.04494122 2.06280654 3.12052167 0.14828718 4.82394477 2.57664118
## [55] 4.27256903 2.80450782 1.98090868 2.13515942 2.20262736 1.48689738
## [61] 3.64713288 1.18777077 2.42550234 4.11038395 2.93910128 3.63291615
## [67] 2.05081147 0.04422756 1.01150930 0.82626234 1.59963110 3.75279945
## [73] 4.95160441 4.52880784 4.99965261 2.77581338 3.91199485 0.17897746
## [79] 0.91317727 1.19017855 3.15202399 3.49065802 2.58556733 0.21749477
## [85] 4.46269767 0.39135993 2.95883999 1.92903015 2.29018534 2.23540449
## [91] 1.71062116 2.90961983 3.69909307 2.65715006 3.95811809 0.41443267
## [97] 3.88899349 2.94599164 2.61265889 4.18648496
hist(runif(100,0,5))

#rnorm() #normal dağılan sayı üret
hist(rnorm(100,0,5))

ad <- c("Ali","Elif","Su","Deniz","Aras",
"Berk","Can","Ece","Efe","Arda")
boy <- c(160,165,170,155,167,162,169,158,160,164)
kilo <- c(55,55,57,50,48,65,58,62,45,47)
BKI <- kilo/(boy/100)^2
BKI
## [1] 21.48437 20.20202 19.72318 20.81165 17.21109 24.76757 20.30741 24.83576
## [9] 17.57812 17.47472
round(mean(BKI),2)
## [1] 20.44
summary(BKI)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 17.21 18.11 20.25 20.44 21.32 24.84
table(boy)
## boy
## 155 158 160 162 164 165 167 169 170
## 1 1 2 1 1 1 1 1 1
harf5<- letters[1:5]
(harf51 <- paste(harf5,1:5,sep="_"))
## [1] "a_1" "b_2" "c_3" "d_4" "e_5"
(harf52 <- paste(harf5,1:5,sep="_",collapse=" "))
## [1] "a_1 b_2 c_3 d_4 e_5"
set.seed(10)
paste(paste(1:10,sep=".", " maddenin guclugu"),paste(sep=":", round(runif(10),2)))
## [1] "1. maddenin guclugu 0.51" "2. maddenin guclugu 0.31"
## [3] "3. maddenin guclugu 0.43" "4. maddenin guclugu 0.69"
## [5] "5. maddenin guclugu 0.09" "6. maddenin guclugu 0.23"
## [7] "7. maddenin guclugu 0.27" "8. maddenin guclugu 0.27"
## [9] "9. maddenin guclugu 0.62" "10. maddenin guclugu 0.43"
ad_soyad<- c("Ayse-Sel","Can-Yucel","Cem-Togay","Banu-Cift")
unlist(strsplit(ad_soyad,"-"))[c(1,3,5,7)]
## [1] "Ayse" "Can" "Cem" "Banu"
unlist(strsplit(ad_soyad,"-"))[c(2,4,6,8)]
## [1] "Sel" "Yucel" "Togay" "Cift"
ad_soyad<- c("Ayse-Sel","Can-Yucel",
"Cem-Togay","Banu-Cift")
ad_soyad_acik <- strsplit(ad_soyad,"-")
ad <- unlist(ad_soyad_acik)[c(1,3,5,7)]
soyad <- unlist(ad_soyad_acik)[c(2,4,6,8)]
liste_eleman <-function(x){x[1]}
unlist(lapply(ad_soyad_acik,function(x){x[1]}))
## [1] "Ayse" "Can" "Cem" "Banu"
unlist(lapply(ad_soyad_acik,function(x){x[2]}))
## [1] "Sel" "Yucel" "Togay" "Cift"
#Kendinizi Test Edin
#S1
ogrenciler <- c("Ogrenci1", "Ogrenci2", "Ogrenci3", "Ogrenci4", "Ogrenci5")
vize <- c(50, 55, 60, 70, 80)
final <- c(45, 65, 85, 90, 85)
gecme_notlari <- (vize + final) / 2
summary(gecme_notlari)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 47.5 60.0 72.5 68.5 80.0 82.5
gecme_notu_min <- min(gecme_notlari)
gecme_notu_min
## [1] 47.5
gecme_notu_ort <- mean(gecme_notlari)
gecme_notu_ort
## [1] 68.5
gecme_notu_maks <- max(gecme_notlari)
gecme_notu_maks
## [1] 82.5
#S2
toplam <- function(x) {
return(x*(x+1)/2)
}
toplam(5)
## [1] 15
#S3
#toplam<- function(){
# x<-readline("Kaca kadar olan sayıların toplamı hesaplansın:")
# sonuc <- sum(1:x)
# paste(x,"'e kadar olan sayıların toplamı:",sonuc)
#}
#toplam()
# Bölüm Sonu 2.1
#a
isim <- c("Ali","Defne","Meltem","Semih","Sevda","Gizem","Emre","Zeynep","Utku","Beril")
#b
vize <- c(60, 70, 50, 80, 65, 70, 85, 70, 92, 80)
#c
final <- c(70, 65, 50, 45, 67, 75, 80, 85, 95, 85)
#d
names(vize)<-isim
names(final)<-isim
#e
vize["Ali"]
## Ali
## 60
final["Ali"]
## Ali
## 70
vize["Beril"]
## Beril
## 80
final["Beril"]
## Beril
## 85
#f
mean(vize);sd(vize)
## [1] 72.2
## [1] 12.40788
mean(final);sd(final)
## [1] 71.7
## [1] 15.74131
#g
summary(vize)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 50.00 66.25 70.00 72.20 80.00 92.00
summary(final)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 45.00 65.50 72.50 71.70 83.75 95.00
#h
vize>90
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
final>90
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#i
vize<50
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
final<50
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#j
finalyuksek <- isim[final>vize]
finalyuksek
## [1] "Ali" "Sevda" "Gizem" "Zeynep" "Utku" "Beril"
#k
gecen <- final>=50
vize_y <- vize[gecen]
final_y <- final[gecen]
vize_y
## Ali Defne Meltem Sevda Gizem Emre Zeynep Utku Beril
## 60 70 50 65 70 85 70 92 80
final_y
## Ali Defne Meltem Sevda Gizem Emre Zeynep Utku Beril
## 70 65 50 67 75 80 85 95 85
#l
length(vize_y)
## [1] 9
length(final_y)
## [1] 9
#m
which.max(vize)
## Utku
## 9
vize
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## 60 70 50 80 65 70 85 70 92 80
which.max(final)
## Utku
## 9
#n????
#o
not<-0.6*final+0.4*vize
not
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## 66.0 67.0 50.0 59.0 66.2 73.0 82.0 79.0 93.8 83.0
summary(not)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 50.00 66.05 70.00 71.90 81.25 93.80
#p
not_s <- sort(not, decreasing = T)
not_s
## Utku Beril Emre Zeynep Gizem Defne Sevda Ali Semih Meltem
## 93.8 83.0 82.0 79.0 73.0 67.0 66.2 66.0 59.0 50.0
#q
class(vize)
## [1] "numeric"
class(isim)
## [1] "character"
class(not)
## [1] "numeric"
class(gecen)
## [1] "logical"
#r
not > mean(not)
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
#s
not==66
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
not==72
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
not==88
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#2.2
round(seq(from=1,to=20,length.out=40),1)
## [1] 1.0 1.5 2.0 2.5 2.9 3.4 3.9 4.4 4.9 5.4 5.9 6.4 6.8 7.3 7.8
## [16] 8.3 8.8 9.3 9.8 10.3 10.7 11.2 11.7 12.2 12.7 13.2 13.7 14.2 14.6 15.1
## [31] 15.6 16.1 16.6 17.1 17.6 18.1 18.5 19.0 19.5 20.0
#2.3
paste("ogrenci",1:10,sep="_")
## [1] "ogrenci_1" "ogrenci_2" "ogrenci_3" "ogrenci_4" "ogrenci_5"
## [6] "ogrenci_6" "ogrenci_7" "ogrenci_8" "ogrenci_9" "ogrenci_10"
#diğer ikisini anlamadım
#2.4
cins<-c("E","K","K","E","K","K","E","K","E","K")
cinssay<-as.numeric(cins)
## Warning: NAs introduced by coercion
cinskar<-as.factor(cins)
levels(cinssay)
## NULL
levels(cinskar)
## [1] "E" "K"
#2.5
harf_not_fonk <- function(not){
if(not>=90) {
return("A")
} else if(not>=80) {
return("B")
} else if (not>=70) {
return("C")
} else if (not>=60) {
return("D")
} else if (not>=50) {
return("E")
}
}
harf_not <- sapply(not, harf_not_fonk)
harf_not
## Ali Defne Meltem Semih Sevda Gizem Emre Zeynep Utku Beril
## "D" "D" "E" "E" "D" "C" "B" "C" "A" "B"
harf_not <- factor(harf_not,levels = c("E","D","C","B","A"),ordered = T )
str(harf_not)
## Ord.factor w/ 5 levels "E"<"D"<"C"<"B"<..: 2 2 1 1 2 3 4 3 5 4
## - attr(*, "names")= chr [1:10] "Ali" "Defne" "Meltem" "Semih" ...
#6
sonnotlar <- data.frame(vize,final,not)
rownames(sonnotlar)<-isim
sonnotlar
## vize final not
## Ali 60 70 66.0
## Defne 70 65 67.0
## Meltem 50 50 50.0
## Semih 80 45 59.0
## Sevda 65 67 66.2
## Gizem 70 75 73.0
## Emre 85 80 82.0
## Zeynep 70 85 79.0
## Utku 92 95 93.8
## Beril 80 85 83.0
genelort<-c(mean(vize),mean(final),mean(not))
son_notlar <- rbind(sonnotlar,genelort)
harf_not[11]<- NA
son_notlar$harf_notu <- harf_not
son_notlar
## vize final not harf_notu
## Ali 60.0 70.0 66.0 D
## Defne 70.0 65.0 67.0 D
## Meltem 50.0 50.0 50.0 E
## Semih 80.0 45.0 59.0 E
## Sevda 65.0 67.0 66.2 D
## Gizem 70.0 75.0 73.0 C
## Emre 85.0 80.0 82.0 B
## Zeynep 70.0 85.0 79.0 C
## Utku 92.0 95.0 93.8 A
## Beril 80.0 85.0 83.0 B
## 11 72.2 71.7 71.9 <NA>