---
title: "02 Data Sets. Crash Course in Statistics (Summer 2025)"
subtitle: "Neuroscience Center Zurich, University of Zurich"
author: "Dr. Zofia Baranczuk"
date: "2025-08-25"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```



## 0. Load required packages
```{r  packages, message=FALSE}
# Run once (in Console) if needed:
# install.packages(c("readr","here"))  

library(readr)  # modern CSV readers
library(here)   # project-safe file paths
```

## 1. Read a CSV (depression-rates-by-country-2025.csv) from the web (using readr)

```{r}
depression_web <- 
  read_csv("https://user.math.uzh.ch/baranczuk/znz/Data/depression-rates-by-country-2025.csv")

```

## 2. Read a local CSV (if downloaded and saved to project folder). 
You need to start a project and install "here". Alternatively, see 3.
Make sure './Data/GDP.csv' exists and that you created a project.
```{r}
GDP <- read_csv(here("Data", "GDP.csv"), show_col_types = FALSE) 
# smoother to work with tidyverse, more on that in 04, returns a 'tibble'
head(GDP)
#View(GDP)

GDP_df <- read.csv(here("Data", "GDP.csv")) 
head(GDP_df) # traditional way of reading in the data, returns a data frame
```

## 3. Reading the local data without using the package 'here'. 

```{r}
GDP_3 <- read_csv("../Data/GDP.csv", show_col_types = FALSE)
#View(GDP_3)

GDP_df3 <- read.csv("../Data/GDP.csv")
#View(GDP_df3) 
```
## 4. Different separators
```{r}

elephant <- read_tsv("../Data/elephant.txt", show_col_types = FALSE)
 # check what happens when you use read_csv.
 # Or you can use read.csv and set sep = "\t".
head(elephant)

```


## 5. Read .RData
```{r}
# load() returns the name(s) of the object(s) it created:
dn <- load(here("Data", "Diag1.RData"))
# or:
# dn <- load("../Data/Diag1.RData")

print(dn)        # see which object was loaded (e.g., "Diag1")

head(Diag1)

```


## 6. Load a built-in dataset
```{r}
data("sleep")
head(sleep)
```

## 7. Load data from a package.

```{r}
library(faraway)
data("anaesthetic")
head(anaesthetic)
```