| tutorial-id |
none |
data-import |
| name |
question |
Abdul Hannan |
| email |
question |
abdul.hannan20008@gmail.com |
| reading-data-from-a-file-1 |
question |
Documentation for package ‘readr’ version 2.1.5 |
| reading-data-from-a-file-2 |
exercise |
library(readr)
file_path <- "data/students.csv"
students_data <- read_csv(file = file_path)
print(head(students_data))
print(str(students_data)) |
| reading-data-from-a-file-3 |
exercise |
library(readr)
file_path <- "data/students.csv"
students <- read_csv(file = file_path)
print(head(students))
print(str(students)) |
| reading-data-from-a-file-4 |
exercise |
students |
| reading-data-from-a-file-5 |
exercise |
library(readr)
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", ""))
students |
| reading-data-from-a-file-6 |
exercise |
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", "")) %>%
rename(student_id = "Student ID")
students |
| reading-data-from-a-file-7 |
exercise |
library(janitor)
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", "")) %>%
rename(student_id = "Student ID")
students |
| reading-data-from-a-file-8 |
exercise |
library(janitor)
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", "")) %>%
rename(student_id = "Student ID") %>%
clean_names()
students |
| reading-data-from-a-file-9 |
exercise |
library(janitor)
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", "")) %>%
rename(student_id = "Student ID") %>%
clean_names() %>%
mutate(meal_plan = factor(meal_plan))
students |
| reading-data-from-a-file-10 |
exercise |
library(janitor)
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", "")) %>%
rename(student_id = "Student ID") %>%
clean_names() %>%
mutate(meal_plan = factor(meal_plan),
age = if_else(age == "five", "5", age),
age = as.numeric(age))
students |
| reading-data-from-a-file-11 |
exercise |
library(janitor)
file_path <- "data/students.csv"
students <- read_csv(file = file_path, na = c("N/A", "")) %>%
rename(student_id = "Student ID") %>%
clean_names() %>%
mutate(meal_plan = factor(meal_plan),
age = if_else(age == "five", "5", age),
age = parse_number(age))
students |
| reading-data-from-a-file-12 |
exercise |
library(readr)
file_path_test <- "data/test_1.csv"
test_data <- read_csv(file = file_path_test)
test_data |
| reading-data-from-a-file-13 |
exercise |
library(readr)
file_path_test <- "data/test_1.csv"
test_data <- read_csv(file = file_path_test, show_col_types = FALSE)
test_data |
| reading-data-from-a-file-14 |
exercise |
library(readr)
file_path_test <- "data/test_1.csv"
test_data <- read_csv(file = file_path_test, show_col_types = FALSE)
print(test_data)
# Read test_2.csv, skipping the first two rows
file_path_test_2 <- "data/test_2.csv"
test_data_2 <- read_csv(file = file_path_test_2, skip = 2, show_col_types = FALSE)
test_data_2 |
| reading-data-from-a-file-15 |
exercise |
file_path_test_3 <- "data/test_3.csv"
test_data_3 <- read_csv(file = file_path_test_3, col_names = FALSE)
test_data_3 |
| reading-data-from-a-file-16 |
exercise |
file_path_test_3 <- "data/test_3.csv"
test_data_3 <- read_csv(file = file_path_test_3, col_names = c("a", "b", "c"))
test_data_3 |
| reading-data-from-a-file-17 |
exercise |
file_path_test_3 <- "data/test_3.csv"
test_data_3 <- read_csv(file = file_path_test_3, col_names = c("a", "b", "c"), col_types = cols(a = col_double(), b = col_double(), c = col_double()))
test_data_3 |
| reading-data-from-a-file-18 |
exercise |
file_path_test_5 <- "data/test_5.csv"
test_data_5 <- read_csv(file = file_path_test_5, na = ".", show_col_types = FALSE) %>%
as.data.frame()
test_data_5 |
| reading-data-from-a-file-19 |
exercise |
file_path_test_6 <- "data/test_6.csv"
test_data_6 <- read_csv(file = file_path_test_6, comment = "#") # Removed show_col_types = FALSE and as.data.frame()
test_data_6 |
| reading-data-from-a-file-20 |
exercise |
file_path_test_7 <- "data/test_7.csv"
test_data_7 <- read_csv(file = file_path_test_7, col_types = cols(grade = col_integer(), student = col_character()))
test_data_7 |
| reading-data-from-a-file-21 |
exercise |
file_path_bad_names <- "data/test_bad_names.csv"
test_bad_names <- read_csv(file = file_path_bad_names, name_repair = "universal")
test_bad_names |
| reading-data-from-a-file-22 |
exercise |
library(janitor)
file_path_bad_names <- "data/test_bad_names.csv"
test_bad_names <- read_csv(file = file_path_bad_names) %>%
clean_names()
test_bad_names |
| reading-data-from-a-file-23 |
exercise |
library(janitor)
file_path_bad_names <- "data/test_bad_names.csv"
test_bad_names <- read_csv(file = file_path_bad_names, name_repair = janitor::make_clean_names)
test_bad_names |
| reading-data-from-a-file-24 |
exercise |
file_path_delim_1 <- "data/delim_1.txt"
delim_data <- read_delim(file = file_path_delim_1, delim = "|", comment = "##", show_col_types = FALSE)
delim_data |
| reading-data-from-a-file-25 |
exercise |
file_path_delim_2 <- "data/delim_2.txt"
delim_data_2 <- read_delim(
file = file_path_delim_2,
delim = "|",
comment = "##",
col_types = cols(
date = col_date(),
population = col_integer(),
town = col_character()
)
)
delim_data_2 |
| controlling-column-types-1 |
exercise |
read_csv("
a, b, c
1, 2, 3") |
| controlling-column-types-2 |
exercise |
read_csv("
logical,numeric,date,string
TRUE,1,2021-01-15,abc
false,4.5,2021-02-15,def
T,Inf,2021-02-16,ghi
") |
| controlling-column-types-3 |
exercise |
simple_csv <- "
x
10
.
20
30"
read_csv(simple_csv) |
| controlling-column-types-4 |
exercise |
simple_csv <- "
x
10
.
20
30"
simple_data <- read_csv(file = simple_csv, col_types = list(x = col_double()))
print(simple_data) |
| controlling-column-types-5 |
exercise |
simple_csv <- "
x
10
.
20
30"
simple_data <- read_csv(file = simple_csv, col_types = list(x = col_double()))
print(simple_data) |
| controlling-column-types-6 |
exercise |
simple_csv <- "
x
10
.
20
30"
simple_data <- read_csv(file = simple_csv, col_types = list(x = col_double()), na = ".")
simple_data |
| controlling-column-types-7 |
exercise |
another_csv <- "
x,y,z
1,2,3"
another_data <- read_csv(file = another_csv, col_types = cols(.default = col_character()))
print(another_data) |
| controlling-column-types-8 |
exercise |
another_csv <- "
x,y,z
1,2,3"
another_data <- read_csv(file = another_csv, col_types = cols_only(x = col_character()))
print(another_data) |
| controlling-column-types-9 |
exercise |
file_path_ex_2 <- "data/ex_2.csv"
ex_2_data <- read_csv(file = file_path_ex_2)
ex_2_data |
| controlling-column-types-10 |
exercise |
file_path_ex_2 <- "data/ex_2.csv"
ex_2_data <- read_csv(file = file_path_ex_2, col_types = cols(.default = col_character()))
ex_2_data |
| controlling-column-types-11 |
exercise |
file_path_ex_2 <- "data/ex_2.csv"
ex_2_data <- read_csv(file = file_path_ex_2, col_types = cols(.default = col_character())) %>%
mutate(a = parse_integer(a))
ex_2_data |
| controlling-column-types-12 |
exercise |
file_path_ex_2 <- "data/ex_2.csv"
ex_2_data <- read_csv(file = file_path_ex_2, col_types = cols(.default = col_character())) %>%
mutate(a = parse_integer(a),
b = parse_date(b, format = "%Y%M%D"))
ex_2_data |
| controlling-column-types-13 |
exercise |
file_path_ex_3 <- "data/ex_3.csv"
ex_3_data <- read_csv(file = file_path_ex_3)
ex_3_data |
| controlling-column-types-14 |
exercise |
file_path_ex_3 <- "data/ex_3.csv"
ex_3_data <- read_csv(file = file_path_ex_3) %>%
mutate(x = parse_date(x, "%d %B %Y"))
ex_3_data |
| controlling-column-types-15 |
exercise |
file_path_ex_3 <- "data/ex_3.csv"
ex_3_data <- read_csv(file = file_path_ex_3) %>%
mutate(x = parse_date(x, "%d %B %Y"),
z = parse_number(z))
ex_3_data |
| reading-data-from-multiple-fil-1 |
exercise |
list.files("data") |
| reading-data-from-multiple-fil-2 |
exercise |
list.files("data", pattern = "similar") |
| reading-data-from-multiple-fil-3 |
exercise |
list.files("data", pattern = "similar",full.names=TRUE) |
| reading-data-from-multiple-fil-4 |
exercise |
combined_data <- list.files("data", pattern = "similar", full.names = TRUE) %>%
map_dfr(~read_csv(.x, na = "."))
print(combined_data) |
| reading-data-from-multiple-fil-5 |
exercise |
combined_data <- list.files("data", pattern = "similar", full.names = TRUE) %>%
map_dfr(~read_csv(.x, na = "."))
combined_data |
| reading-data-from-multiple-fil-6 |
exercise |
similar_files <- list.files("data", pattern = "similar", full.names = TRUE)
combined_similar_data <- similar_files %>%
map_df(~read_csv(.x, na = ".", show_col_types = FALSE))
combined_similar_data |
| reading-data-from-multiple-fil-7 |
exercise |
list.files(path = "data", pattern = "sales") |
| reading-data-from-multiple-fil-8 |
exercise |
list.files(path = "data", pattern = "sales",full.names = TRUE) |
| reading-data-from-multiple-fil-9 |
exercise |
combined_sales <- list.files(path = "data", pattern = "sales", full.names = TRUE) %>%
map_dfr(read_csv, id = "file")
combined_sales |
| writing-to-a-file-1 |
exercise |
students2 <- students |>
clean_names() |>
mutate(
meal_plan = factor(meal_plan),
age = if_else(age == "five", "5", age),
age = parse_number(age)
)
students2 |
| writing-to-a-file-2 |
exercise |
students2 |
| writing-to-a-file-3 |
exercise |
write_csv(x = students2, file = "data/students2.csv") |
| writing-to-a-file-4 |
exercise |
students2_read <- read_csv("data/students2.csv")
students2_read |
| writing-to-a-file-5 |
exercise |
iris_p <- iris |>
ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
geom_jitter() +
labs(title = "Sepal Dimensions of Various Species of Iris",
x = "Sepal Length",
y = "Sepal Width")
write_rds(iris_p, "data/test_1.rds") |
| writing-to-a-file-6 |
exercise |
list.files("data",include.dirs=TRUE) |
| writing-to-a-file-7 |
exercise |
iris_p_loaded <- read_rds("data/test_1.rds")
iris_p_loaded |
| writing-to-a-file-8 |
exercise |
write_rds(mtcars, "data/test_2.rds") |
| writing-to-a-file-9 |
exercise |
list.files("data") |
| writing-to-a-file-10 |
exercise |
mtcars_read <- read_rds("data/test_2.rds")
mtcars_read |
| writing-to-a-file-11 |
question |
# Load the arrow package
library(arrow)
# Save a dataset to a Parquet file
write_parquet(mtcars, "data/mtcars.parquet")
# Read it back
mtcars_parquet <- read_parquet("data/mtcars.parquet") |
| data-entry-1 |
exercise |
library(tibble)
my_tibble <- tibble(
x = c(1, 2, 5),
y = c("h", "m", "g"),
z = c(0.08, 0.83, 0.60)
)
my_tibble |
| data-entry-2 |
exercise |
library(tibble)
my_tribble <- tribble(
~x, ~y, ~z,
1, "h", 0.08,
2, "m", 0.83,
5, "g", 0.60
)
my_tribble |
| minutes |
question |
150 |