-
Notifications
You must be signed in to change notification settings - Fork 10
/
ch7_exercise.Rmd
94 lines (65 loc) · 2.76 KB
/
ch7_exercise.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
title: "活用資料框 - 隨堂練習"
author: "Yao-Jen Kuo"
date: "`r Sys.Date()`"
output:
html_document:
self_contained: false
---
```{r, include=FALSE}
tutorial::go_interactive()
```
## 轉換變數類型
練習把一個文字(character)轉換成 Date 格式。
```{r ex="char_transform", type="sample-code"}
# Create the character
boston_marathon_2016 <- "2016-04-18"
# Check the variable type using `class()`
# Using as.Date() to transform the character and re-assign it to another variable `bm_2016_date`
bm_2016_date <-
# Check the newly created variable type using `class()`
```
```{r ex="char_transform", type="solution"}
# Create the character
boston_marathon_2016 <- "2016-04-18"
# Check the variable type using `class()`
class(boston_marathon_2016)
# Using as.Date() to transform the character and re-assign it to another variable `bm_2016_date`
bm_2016_date <- as.Date(boston_marathon_2016)
# Check the newly created variable type using `class()`
class(bm_2016_date)
```
```{r ex="char_transform", type="sct"}
test_output_contains("class(boston_marathon_2016)", incorrect_msg = "Did you use `class()` on boston_marathon_2016?")
test_object("bm_2016_date", incorrect_msg = "Did you transform the character variable to date correctly?")
test_output_contains("class(bm_2016_date)", incorrect_msg = "Did you use `class()` on bm_2016_date?")
```
## 聯結資料框
練習將兩個資料框 `order_data` 與 `customer_data` 進行內部合併。
```{r ex="merge_dfs", type="pre-exercise-code"}
order_data <- read.csv("http://storage.googleapis.com/r_rookies/merge_1.csv")
customer_data <- read.csv("http://storage.googleapis.com/r_rookies/merge_2.csv")
```
```{r ex="merge_dfs", type="sample-code"}
# Print order_data which was loaded previously
# Print customer_data which was loaded previously
# Merge these 2 data frames using their common variable: CustomerID and assign to a new data frame: merged_data
merged_data <- merge(___, ___)
# Print merged_data to the console
```
```{r ex="merge_dfs", type="solution"}
# Print order_data which was loaded previously
order_data
# Print customer_data which was loaded previously
customer_data
# Merge these 2 data frames using their common variable: CustomerID and assign to a new data frame: merged_data
merged_data <- merge(order_data, customer_data)
# Print merged_data to the console
merged_data
```
```{r ex="merge_dfs", type="sct"}
test_output_contains("order_data", incorrect_msg="Did you print out `order_data` to the console?")
test_output_contains("customer_data", incorrect_msg="Did you print out `customer_data` to the console?")
test_object("merged_data", incorrect_msg="Did you merge the data correctly?")
test_output_contains("merged_data", incorrect_msg="Did you print out `merged_data` to the console?")
```