Accessing Datasets from R-Studio Packages

1.a - Open the Datasets Package in R-Studio

{r} library(datasets)

1.b - Load dataset called sleep from the Datasets Package

{r} data(sleep)

1.c - Save the original sleep dataset into R as an object & make a copy that we will work with

{r} sleep_original <- sleep # preserve the original sleep_copy <- sleep # working copy

Characteristics/Descriptions of the Sleep Dataset

Part 1

2.a. How many rows are in the sleep dataset?

{r} nrow(sleep_copy)

2.b How many columns are in the sleep dataset?

{r} ncol(sleep_copy)

2.c What are the names of the columns in the sleep dataset?

{r} names(sleep_copy)

2.d What type of variables are the 3 variables in the sleep dataset?

{r} str(sleep_copy)

Characteristics/Descriptions of the Sleep Dataset

PART 2

3.a Run head() & tail() on the Sleep Dataset

{r} head(sleep_copy) tail(sleep_copy)

3.b Run summary() on the Sleep Dataset

{r} summary(sleep_copy)

Characteristics/Descriptions of the Sleep Dataset

PART 3

4.a Run describe() on the sleep dataset

{r} library(psych) # psych package provides describe() and describe.by() describe(sleep_copy)

4.b Run describe.by() grouped by the variable “group”

{r} describe.by(sleep_copy, group = sleep_copy$group)

Opening Datasets from Local Files

5.a Open both Stat 200 datasets

```{r} library(readxl)

Excel Version — update the path to match where your file is saved

stat200_xlsx <- read_excel(“stat200_dataset.xlsx”)

CSV Version — update the path to match where your file is saved

stat200_csv <- read.csv(“stat200_dataset.csv”)


# 5.b Save originals and make working copies for both
```{r}
# Excel-based
stat200_xlsx_original <- stat200_xlsx
stat200_xlsx_copy     <- stat200_xlsx

# CSV-based
stat200_csv_original  <- stat200_csv
stat200_csv_copy      <- stat200_csv