Skip to content

Commit 34e6a84

Browse files
Add data in place of feline-data_v2.csv, closes #717
1 parent 4d666c8 commit 34e6a84

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

episodes/04-data-structures-part1.Rmd

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -164,34 +164,26 @@ No matter how
164164
complicated our analyses become, all data in R is interpreted as one of these
165165
basic data types. This strictness has some really important consequences.
166166

167-
A user has added details of another cat. This information is in the file
168-
`data/feline-data_v2.csv`.
167+
A user has added details of another cat. We can add an additional row to our cats `data.frame` using `rbind`.
169168

170-
```{r, eval=FALSE}
171-
file.show("data/feline-data_v2.csv")
172-
```
173-
174-
```{r, eval=FALSE}
175-
coat,weight,likes_catnip
176-
calico,2.1,1
177-
black,5.0,0
178-
tabby,3.2,1
179-
tabby,2.3 or 2.4,1
169+
```{r}
170+
additional_cat <- data.frame(coat = "tabby", weight = "2.3 or 2.4", likes_catnip = 1)
171+
cats2 <- rbind(cats, additional_cat)
172+
cats2
180173
```
181174

182-
Load the new cats data like before, and check what type of data we find in the
183-
`weight` column:
175+
Let's check what type of data we find in the
176+
`weight` column of our new object:
184177

185178
```{r}
186-
cats <- read.csv(file="data/feline-data_v2.csv")
187-
typeof(cats$weight)
179+
typeof(cats2$weight)
188180
```
189181

190182
Oh no, our weights aren't the double type anymore! If we try to do the same math
191183
we did on them before, we run into trouble:
192184

193185
```{r}
194-
cats$weight + 2
186+
cats2$weight + 2
195187
```
196188

197189
What happened?
@@ -206,7 +198,7 @@ csv file, it is stored as a data frame. We can recognize data frames by the firs
206198
is written by the `str()` function:
207199

208200
```{r}
209-
str(cats)
201+
str(cats2)
210202
```
211203

212204
*Data frames* are composed of rows and columns, where each column has the
@@ -389,8 +381,7 @@ Create a new script in RStudio and copy and paste the following code. Then
389381
move on to the tasks below, which help you to fill in the gaps (\_\_\_\_\_\_).
390382

391383
```
392-
# Read data
393-
cats <- read.csv("data/feline-data_v2.csv")
384+
Using the object `cats2`:
394385
395386
# 1. Print the data
396387
_____
@@ -402,15 +393,15 @@ _____(cats)
402393
# The correct data type is: ____________.
403394
404395
# 4. Correct the 4th weight data point with the mean of the two given values
405-
cats$weight[4] <- 2.35
396+
cats2$weight[4] <- 2.35
406397
# print the data again to see the effect
407398
cats
408399
409400
# 5. Convert the weight to the right data type
410-
cats$weight <- ______________(cats$weight)
401+
cats2$weight <- ______________(cats2$weight)
411402
412403
# Calculate the mean to test yourself
413-
mean(cats$weight)
404+
mean(cats2$weight)
414405
415406
# If you see the correct mean value (and not NA), you did the exercise
416407
# correctly!
@@ -420,7 +411,7 @@ mean(cats$weight)
420411

421412
#### 1\. Print the data
422413

423-
Execute the first statement (`read.csv(...)`). Then print the data to the
414+
Print the data to the
424415
console
425416

426417
::::::::::::::: solution
@@ -435,8 +426,8 @@ Show the content of any variable by typing its name.
435426
Two correct solutions:
436427

437428
```
438-
cats
439-
print(cats)
429+
cats2
430+
print(cats2)
440431
```
441432

442433
:::::::::::::::::::::::::
@@ -445,7 +436,7 @@ print(cats)
445436

446437
The data type of your data is as important as the data itself. Use a
447438
function we saw earlier to print out the data types of all columns of the
448-
`cats` table.
439+
`cats2` `data.frame`.
449440

450441
::::::::::::::: solution
451442

@@ -462,15 +453,14 @@ here.
462453
> ### Solution to Challenge 1.2
463454
>
464455
> ```
465-
> str(cats)
456+
> str(cats2)
466457
> ```
467458
468459
#### 3\. Which data type do we need?
469460
470461
The shown data type is not the right one for this data (weight of
471462
a cat). Which data type do we need?
472463
473-
- Why did the `read.csv()` function not choose the correct data type?
474464
- Fill in the gap in the comment with the correct data type for cat weight!
475465
476466
::::::::::::::: solution
@@ -549,8 +539,8 @@ auto-complete function: Type "`as.`" and then press the TAB key.
549539
> There are two functions that are synonymous for historic reasons:
550540
>
551541
> ```
552-
> cats$weight <- as.double(cats$weight)
553-
> cats$weight <- as.numeric(cats$weight)
542+
> cats2$weight <- as.double(cats2$weight)
543+
> cats2$weight <- as.numeric(cats2$weight)
554544
> ```
555545
556546
::::::::::::::::::::::::::::::::::::::::::::::::::

0 commit comments

Comments
 (0)