Skip to content

Commit

Permalink
Merge pull request swcarpentry#793 from Aariq/patch-1
Browse files Browse the repository at this point in the history
Update 05-data-structures-part2.Rmd
  • Loading branch information
matthieu-bruneaux committed Apr 20, 2023
2 parents c710d13 + 097e73d commit 710a2e5
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions episodes/05-data-structures-part2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ str(cats)
> > ## Solution to Challenge 1
> > 1. `human_age <- cats$age * 7`
> > 2. `human_age <- factor(human_age)`. `as.factor(human_age)` works just as well.
> > 3. `as.numeric(human_age)` yields `1 2 3 4 4` because factors are stored as integers (here, 1:4), each of which is associated with a label (here, 28, 35, 56, and 63). Converting the factor to a numeric vector gives us the underlying integers, not the labels. If we want the original numbers, we need to convert `human_age` to a character vector (using `as.character(human_age)`) and then to a numeric vector (why does this work?). This comes up in real life when we accidentally include a character somewhere in a column of a .csv file supposed to only contain numbers, and forget to set `stringsAsFactors=FALSE` when we read in the data.
> > 3. `as.numeric(human_age)` yields `1 2 3 4 4` because factors are stored as integers (here, 1:4), each of which is associated with a label (here, 28, 35, 56, and 63). Converting the factor to a numeric vector gives us the underlying integers, not the labels. If we want the original numbers, we need to convert `human_age` to a character vector (using `as.character(human_age)`) and then to a numeric vector (why does this work?). This comes up in real life when we accidentally include a character somewhere in a column of a .csv file supposed to only contain numbers, and set `stringsAsFactors=TRUE` when we read in the data.
> {: .solution}
{: .challenge}

Expand Down Expand Up @@ -210,8 +210,7 @@ cats
> ```{r}
> df <- data.frame(id = c("a", "b", "c"),
> x = 1:3,
> y = c(TRUE, TRUE, FALSE),
> stringsAsFactors = FALSE)
> y = c(TRUE, TRUE, FALSE))
> ```
> Make a data frame that holds the following information for yourself:
>
Expand All @@ -226,8 +225,7 @@ cats
> > ```{r}
> > df <- data.frame(first = c("Grace"),
> > last = c("Hopper"),
> > lucky_number = c(0),
> > stringsAsFactors = FALSE)
> > lucky_number = c(0))
> > df <- rbind(df, list("Marie", "Curie", 238) )
> > df <- cbind(df, coffeetime = c(TRUE,TRUE))
> > ```
Expand Down

0 comments on commit 710a2e5

Please sign in to comment.