x <- 10
y <- 3.14
x <- 5 > 3
name <- "Ondra"
city <- "Ústí nad Labem"
gender <- factor(c("Male", "Female"))
numbers <- c(1, 2, 3, 4)
Přístup k prvku:
numbers[1]
matrix_data <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
Přístup k prvku:
matrix_data[1, 2] # [řádek, sloupec]
students <- data.frame(name = c("Anna", "Petr"), age = c(20, 21))
Přístup ke sloupci:
students$name
anna_list <- list(name = "Anna", age = 20, grades = c(1, 2, 1))
Přístup k položce:
anna_list$name
5 + 3
5 - 3
5 * 3
5 / 3
5 ^ 2
5 %% 2
5 %/% 2
x == y
x != y
x > y
x < y
x >= y
x <= y
x & y
x | y
!x
name <- "Ondra"
paste("Hello", "World")
Výsledek:
"Hello World"
Bez mezery:
paste0("Hello", "World")
Výsledek:
"HelloWorld"
nchar("Ondra")
tolower("ONDRA")
toupper("ondra")
gsub(
"World",
"R",
"Hello World"
)
strsplit(
"a,b,c",
","
)
grepl(
"high school",
education
)
grepl(
"^high",
education
)
grepl(
"school$",
education
)
grepl(
"a.c",
text
)
Příklady:
abc
axc
a7c
grepl(
"a+",
text
)
Příklady:
a
aa
aaa
grepl(
"[0-9]",
text
)
grepl(
"[A-Za-z]",
text
)
grepl(
"cat|dog",
text
)
Příklady:
cat
dog
data <- read.csv(
"data.csv"
)
data <- read.csv2(
"data.csv"
)
write.csv(
data,
"output.csv",
row.names = FALSE
)
library(readxl)
data <- read_excel(
"data.xlsx"
)
data <- read_excel(
"data.xlsx",
sheet = "Sheet1"
)
library(writexl)
write_xlsx(
data,
"output.xlsx"
)
if (x > 0) {
print("kladné číslo")
}
if (x > 0) {
print("kladné číslo")
} else {
print("záporné číslo nebo nula")
}
if (x > 0) {
print("kladné číslo")
} else if (x < 0) {
print("záporné číslo")
} else {
print("nula")
}
for (i in 1:5) {
print(i)
}
numbers <- c(10, 20, 30)
for (number in numbers) {
print(number)
}
x <- 1
while (x <= 5) {
print(x)
x <- x + 1
}
for (i in 1:10) {
if (i == 5) {
break
}
print(i)
}
for (i in 1:5) {
if (i == 3) {
next
}
print(i)
}
data %>%
select(
name,
age
)
data %>%
rename(
full_name = name
)
data %>%
mutate(
age_plus_one = age + 1
)
data %>%
filter(
age >= 18
)
data %>%
filter(
grepl(
"high school",
education
)
)
data %>%
arrange(age)
data %>%
arrange(desc(age))
data %>%
group_by(gender) %>%
summarise(
avg_age = mean(age)
)
data %>%
group_by(
gender,
race
) %>%
summarise(
avg_age = mean(age),
.groups = "drop"
)
inner_join(
table1,
table2,
by = "id"
)
left_join(
table1,
table2,
by = "id"
)
right_join(
table1,
table2,
by = "id"
)
full_join(
table1,
table2,
by = "id"
)
pivot_wider(
data,
names_from = gender,
values_from = avg.score
)
pivot_longer(
data,
cols = c(
female,
male
),
names_to = "gender",
values_to = "avg.score"
)
table(
data$gender
)
table(
data$gender,
data$race
)
prop.table(
table(data$gender)
)
prop.table(
table(
data$gender,
data$race
),
margin = 1
)
plot(
x = data$height,
y = data$weight
)
Interpretace:
plot(
data$year,
data$value,
type = "l"
)
Interpretace:
barplot(
table(data$gender)
)
Interpretace:
pie(
table(data$gender)
)
Interpretace:
hist(
data$age
)
Interpretace:
Možné tvary:
boxplot(
data$age
)
Pro skupiny:
boxplot(
age ~ gender,
data = data
)
Interpretace:
library(ggplot2)
ggplot(
data,
aes(x = age)
) +
geom_histogram()
ggplot(
data,
aes(
x = gender,
y = age
)
) +
geom_boxplot()
ggplot(
data,
aes(
x = height,
y = weight
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE
)
Interpretace: