You are on page 1of 9

x <- 10:1

y <- -4:5

q <- c("Hockey", "Football", "Baseball", "Curling", "Rugby",

"Lacrosse", "Basketball", "Tennis", "Cricket", "Soccer")

theDF <- data.frame(x, y, q)

theDF

theDF <- data.frame(First=x, Second=y, Sport=q)

nrow(theDF)

ncol(theDF)

dim(theDF)

names(theDF)

names(theDF)[3]

rownames(theDF)

rownames(theDF) <- c("One", "Two", "Three", "Four", "Five", "Six",

"Seven", "Eight", "Nine", "Ten")

rownames(theDF)

rownames(theDF) <- NULL

rownames(theDF)

class(theDF)

theDF$Sport

theDF[3, 2]

theDF[3, 2:3]

theDF[, 3]

theDF[, 2:3]
theDF[2, ]

##Još jedan način da se pristupi određenoj koloni je da se ime njenog stupca (ili njegov broj) koristi ili kao drugi argument u
uglatim zagradama ili kao jedini argument u pojedinačne ili dvostruke uglate zagrade

theDF[, "Sport"]

class(theDF[, "Sport"])

theDF["Sport"]

class(theDF["Sport"])

#Sve ove metode imaju različite izlaze.

#Neki vraćaju vektor; neki vraćaju data-frame u jednom stupc

#u. Da bi se osigurao data.frame u jednoj koloni dok se

#koriste jednostruke uglate zagrade, postoji treći argument:

#drop = FALSE.

theDF[, "Sport", drop=FALSE]

class(theDF[, "Sport", drop=FALSE])

#ova cinjenjica vam je bitna usled operacija koje se izvode nad matricama

#operacije nad listama

list(1, 2, 3)

list(c(1, 2, 3))

(list3 <- list(c(1, 2, 3), 3:7))

list(theDF, 1:10)

list5 <- list(theDF, 1:10, list3)

names(list5)

names(list5) <-c("data.frame", "vector", "list")

names(list5)

length(list5)

list6 <- list(TheDataFrame=theDF, TheVector=1:10, TheList=list3)

names(list6)
#Matrice

#Veoma česta matematička struktura koja je od suštinskog

#značaja za statistiku je matrica. Ovo je slično

#data.frame-u po tome što je pravougaoni sa redovima i kolonama,

#što svaki pojedinačni element, bez obzira na stupac,

#mora biti istog tipa, najčešće sve numeričke.

#Takođe deluju slično vektorima sa sabiranjem po elementima,

#množenjem, oduzimanjem, deljenjem i jednakošću

#Funkcije nrov, ncol i dim funkcionišu baš kao i za data.frames.

#kreirati matricu 5x2

A <- matrix(1:10, nrow=5)

B <- matrix(21:30, nrow=5)

C <- matrix(21:40, nrow=2)

nrow(A)

ncol(A)

dim(A)

A + B #sabiranje

A * B # mnozenje

A == B # provera jedakosti

#Množenje matrice je često korišćena operacija u matematici,

#koja zahteva da broj stupaca leve matrice bude jednak broju

#redova desne matrice. A i B su 5x2, transponovati ćemo B da bi se mogao koristiti sa desne strane.
A %*% t(B)

colnames(A)

rownames(A)

colnames(A) <- c("Left", "Right")

rownames(A) <- c("1st", "2nd", "3rd", "4th", "5th")

colnames(B)

rownames(B)

colnames(B) <- c("First", "Second")

rownames(B) <- c("One", "Two", "Three", "Four", "Five")

colnames(C)

rownames(C)

colnames(C) <- LETTERS[1:10]

rownames(C) <- c("Top", "Bottom")

t(A)

A %*% C

#Niz

#Niz je u osnovi višedimenzionalni vektor

theArray <- array(1:12, dim=c(2, 3, 2))

theArray

theArray[1, , ]

theArray[1, , 1]

theArray[, , 1]
install.packages("readxl")

library("readxl")

diamonds <- read.csv(file = 'C:\\Users\\Agent 47\\Desktop\\diamonds.csv',header = FALSE)

library(ggplot2)

data(diamonds)

head(diamonds)

#Base Histograms

hist(diamonds$carat, main="Carat Histogram", xlab="Carat")

#Base Scatterplot

plot(price ~ carat, data=diamonds)

plot(diamonds$carat, diamonds$price)

#Boxplots

boxplot(diamonds$carat)

#Ideja iza box plot je da debela srednja linija predstavlja srednju vrednost,

#dok kutiju ograničavaju prvi i treći kvartil

# Odnosno, srednjih 50 posto podataka (Interkvartilni opseg ili IKR)

#ggplot2

#ggplot2 Histograms and Densities

ggplot(data=diamonds) + geom_histogram(aes(x=carat))

ggplot(data=diamonds) + geom_density(aes(x=carat), fill="grey50")

#ggplot2 Scatterplots

ggplot(diamonds, aes(x=carat, y=price)) + geom_point()

g <-ggplot(diamonds,aes(x=carat, y=price))

g + geom_point(aes(color=color))

g + geom_point(aes(color=color)) + facet_wrap(~color)
ggplot(diamonds, aes(x=carat)) + geom_histogram() + facet_wrap(~color)

#ggplot2 Boxplots and Violins Plots

ggplot(diamonds,aes(y=carat, x=1)) + geom_boxplot()

ggplot(diamonds, aes(y=carat, x=cut)) + geom_boxplot()

ggplot(diamonds, aes(y=carat, x=cut)) + geom_violin()

#ggplot2 Line Graphs

ggplot(economics, aes(x=date, y=pop)) + geom_line()

as.numeric(TRUE)

as.numeric(FALSE)

1 == 1

1<1

1 <= 1

1>1

1 >= 1

ifelse(1 == 1, "Yes", "No")

ifelse(1 == 0, "Yes", "No")

toTest <- c(1, 1, 0, 1, 0, 1)

ifelse(toTest == 1, "Yes", "No")

ifelse(toTest == 1, toTest*3, toTest)

ifelse(toTest == 1, toTest*3, "Zero")

toTest[2] <- NA

ifelse(toTest == 1, "Yes", "No")

ifelse(toTest == 1, toTest*3, toTest)

ifelse(toTest == 1, toTest*3, "Zero")


a <- c(1, 1, 0, 1)

b <- c(2, 1, 0, 1)

ifelse(a == 1 & b == 1, "Yes", "No")

ifelse(a == 1 && b == 1, "Yes", "No")

#funkcije

double.num <- function(x)

x*2

double.num(5)

# boolean funkcija

check.bool <- function(x)

if(x == 1)

print("hello")

}else

print("goodbye")

check.bool(1)

check.bool(0)
#switch finkcija

use.switch <- function(x)

switch(x,

"a"="first",

"b"="second",

"z"="last",

"c"="third",

"other")

use.switch("b")

use.switch("c")

use.switch("d")

use.switch("e")

use.switch("z")

#FOR funkcija

for(i in 1:10)

print(i)

fruit <- c("apple", "banana", "pomegranate")

fruitLength <- rep(NA, length(fruit))

for(a in fruit)

fruitLength[a] <- nchar(a)

}
fruitLength

theMatrix <- matrix(1:9, nrow=3)

apply(theMatrix, 1, sum)

apply(theMatrix, 2, sum)

rowSums(theMatrix)

colSums(theMatrix)

theList <- list(A=matrix(1:9, 3), B=1:5, C=matrix(1:4, 2), D=2)

theList[["A"]]

theList[["B"]]

theList[["C"]]

theList[["D"]]

lapply(theList, sum)

#funkcija aggregate

data(diamonds, package='ggplot2')

head(diamonds)

aggregate(price ~ cut, diamonds, mean)

aggregate(price ~ cut + color, diamonds, mean)

select(diamonds, 1, 7)

You might also like