170 lines
3.3 KiB
R
170 lines
3.3 KiB
R
|
|
# ZAD 1
|
|
pierwiastek <- function(x){
|
|
return(sqrt(x))
|
|
}
|
|
moja_lista <- list(c("Jakub", "Adamski"), pi, pierwiastek, c(seq(0.02, 1, by=0.02)))
|
|
print(moja_lista)
|
|
|
|
moja_lista[1] <- NULL
|
|
moja_lista[2] <- NULL
|
|
print(moja_lista)
|
|
|
|
print(sapply(moja_lista, gamma))
|
|
|
|
# ZAD 2
|
|
library(Matrix)
|
|
|
|
m = matrix( rev(c(1,5,3,2,0,5,1,2,1)), nrow = 3, ncol = 3)
|
|
print(rankMatrix(m))
|
|
print(det(m))
|
|
print(solve(m))
|
|
print(eigen(m))
|
|
|
|
print(sum(m[1,]))
|
|
print(sum(m[2,]))
|
|
print(sum(m[3,]))
|
|
|
|
print(mean(m[1,]))
|
|
print(mean(m[2,]))
|
|
print(mean(m[3,]))
|
|
|
|
print(m %*% solve(m))
|
|
|
|
# ZAD 3
|
|
ostatnia <- function(x){
|
|
return(x %% 10)
|
|
}
|
|
kwadraty <- c(1:100)^2
|
|
jednosci <- sapply(kwadraty, ostatnia)
|
|
print(table(jednosci))
|
|
|
|
# ZAD 4
|
|
tabliczka <- outer(paste(c(1:5), "*"), paste(c(1:5), "="), c(1:5)*c(1:5), FUN = "paste")
|
|
print(tabliczka)
|
|
|
|
# ZAD 5
|
|
dane <- read.table("dane1.csv", header = TRUE, sep = ";")
|
|
print(dane[c(FALSE, TRUE), ])
|
|
filtrowanie1 <- dane[dane$Wiek > 50, ]
|
|
filtrowanie2 <- filtrowanie1[filtrowanie1$Wezly.chlonne == 1, ]
|
|
print(filtrowanie2)
|
|
|
|
# ZAD 6
|
|
tocelc <- function(x){
|
|
return(((x-32) * 5)/9)
|
|
}
|
|
miesiace <- c("Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień")
|
|
ny_f <- c(32, 33, 41, 52, 62, 72, 77, 75, 68, 58, 47, 35)
|
|
ny_c <- sapply(ny_f, tocelc)
|
|
temperatury <- data.frame(miesiace, ny_f, ny_c)
|
|
names(temperatury)[names(temperatury) == "ny_f"] <- "NY_FARENHEIT"
|
|
names(temperatury)[names(temperatury) == "ny_c"] <- "NY_CELCIUS"
|
|
print(temperatury)
|
|
write.table(temperatury, "NY_temp.RData")
|
|
|
|
# ZAD 1
|
|
x <- 1:10
|
|
iloczyn <- 1
|
|
for (liczba in x){
|
|
iloczyn <- iloczyn * liczba
|
|
}
|
|
print(iloczyn)
|
|
|
|
x <- 1:10
|
|
iloczyn <- 1
|
|
licznik <- 1
|
|
while (licznik <= length(x)){
|
|
iloczyn <- iloczyn * x[licznik]
|
|
licznik <- licznik + 1
|
|
}
|
|
print(iloczyn)
|
|
|
|
x <- 1:10
|
|
iloczyn <- 1
|
|
licznik <- 1
|
|
repeat {
|
|
iloczyn <- iloczyn * x[licznik]
|
|
licznik <- licznik + 1
|
|
if (licznik > length(x)) {
|
|
break
|
|
}
|
|
}
|
|
print(iloczyn)
|
|
|
|
# ZAD 2
|
|
wspolczynnik <- function(n, k){
|
|
mianownik <- factorial(k) * factorial((n-k))
|
|
return(factorial(n)/mianownik)
|
|
}
|
|
licznik <- 0
|
|
for (n in 1:100){
|
|
for (r in 1:n){
|
|
if (wspolczynnik(n, r) > 1000000){
|
|
licznik <- licznik + 1
|
|
}
|
|
}
|
|
}
|
|
print(licznik)
|
|
|
|
# ZAD 3
|
|
x <- c(3, 3, 2, 2, 3, 3)
|
|
y <- c(1,2,3,4)
|
|
|
|
palindrom <- function(x){
|
|
odwr <- rev(x)
|
|
licznik <- 1
|
|
while (licznik <= length(x)){
|
|
if (odwr[licznik] != x[licznik]){
|
|
return(FALSE)
|
|
}
|
|
licznik <- licznik + 1
|
|
}
|
|
return(TRUE)
|
|
}
|
|
|
|
print(palindrom(x))
|
|
print(palindrom(y))
|
|
|
|
# ZAD 4
|
|
torad <- function(x){
|
|
return(x * pi / 180)
|
|
}
|
|
|
|
katy <- c(0, 30, 45, 60, 90)
|
|
sinn <- sapply(sapply(katy, torad), sin)
|
|
coss <- sapply(sapply(katy, torad), cos)
|
|
tann <- sapply(sapply(katy, torad), tan)
|
|
ctgg <- 1 / sapply(sapply(katy, torad), tan)
|
|
tab <- data.frame(sinn, coss, tann, ctgg)
|
|
print(tab)
|
|
|
|
|
|
# ZAD 5
|
|
x <- c(1,2,3,4,5,6,7)
|
|
|
|
minmax <- function(x){
|
|
if (length(x) < 3){
|
|
return("za krótki argument")
|
|
}
|
|
liczby <- c()
|
|
pom <- x
|
|
for(licznik in 1:3){
|
|
mini <- min(pom)
|
|
pom <- c(pom[1:match(mini, pom)-1], pom[(match(mini, pom)+1):length(pom)])
|
|
liczby <- c(liczby, mini)
|
|
}
|
|
pom <- x
|
|
for(licznik in 1:3){
|
|
maxi <- max(pom)
|
|
pom <- c(pom[1:match(maxi, pom)-1], pom[(match(maxi, pom)+1):length(pom)+1])
|
|
pom <- pom[!is.na(pom)]
|
|
liczby <- c(liczby, maxi)
|
|
}
|
|
return(liczby)
|
|
}
|
|
|
|
print(minmax(x))
|
|
|
|
|