library(tidyverse) library(edr)
a_number
.a_number <- 5 a_number
three_numbers
.three_numbers <- c(1, 2, 3) three_numbers
one_word
.one_word <- "hello" one_word
one_word
variable with a different value.one_word <- "hi" one_word
library(tidyverse) library(edr)
sw
dataset can be printed to the console by using sw
.sw
filter()
function, and filtering by a single value in the species
column of the sw
dataset.filter(sw, species == "Droid")
height
column has values greater than 220.filter(sw, height > 220)
height
above 210, or, mass
above 120.filter(sw, height > 210 | mass > 120)
Droid
and Human
characters (method 1 with a [expr | expr] construction).filter(sw, species == "Droid" | species == "Human")
Droid
and Human
characters (method 2 with [colname %>% c(...)]
construction).filter(sw, species %in% c("Droid", "Human"))
arrange(sw, height)
arrange(sw, hair_color, gender)
hair_color
, then by ascending gender
.arrange(sw, desc(hair_color), gender)
name
, gender
, and species
columns.select(sw, name, gender, species)
gender
, species
, and names
columns (in that order).select(sw, gender, species, name)
name
column and any additional columns ending with "s".select(sw, name, ends_with("s"))
name
column and any additional columns containing a "_" character.select(sw, name, contains("_"))
everything()
else after that.select(sw, name, homeworld, gender, everything())
sw_small
table containing just three columns.sw_small <- select(sw, name, height, mass) sw_small
bmi
column to sw_small
using the mutate()
function.mutate(sw_small, bmi = mass / (height / 100)^2)
bmi
column as before and then create bmi_rnd
.mutate( sw_small, bmi = mass / (height / 100)^2, bmi_rnd = round(bmi, 0) )
species
variable and summarizing to get the mean mass.# Create grouped data; where the table is grouped by `species` by_species <- group_by(sw, species) # Create a data summary that gets the mean mass by `species` summarize(by_species, avg_mass = mean(mass))
NA
) values.summarize(by_species, avg_mass = mean(mass, na.rm = TRUE))
# Create grouped data; grouping by `species` and `gender` grouping <- group_by(sw, species, gender) # Summarize to get average mass and average height summarize( grouping, avg_mass = mean(mass, na.rm = TRUE), avg_height = mean(height, na.rm = TRUE) )
# Get average BMI values for humans across the different worlds humans <- filter(sw, species == "Human") humans_bmi <- mutate(humans, bmi = mass / (height / 100)^2) humans_bmi_by_homeworld <- group_by(humans_bmi, homeworld) humans_avg_bmi_by_homeworld <- summarize(humans_bmi_by_homeworld, avg_bmi = mean(bmi, na.rm = TRUE)) humans_avg_bmi_by_homeworld_sorted <- arrange(humans_avg_bmi_by_homeworld, desc(avg_bmi)) humans_avg_bmi_by_homeworld_sorted
bmi_sorted <- sw %>% filter(species == "Human") %>% mutate(bmi = mass / (height / 100)^2) %>% group_by(homeworld) %>% summarize(avg_bmi = mean(bmi, na.rm = TRUE)) %>% arrange(desc(avg_bmi)) bmi_sorted
# Get a table of average BMI values for humans across # the different worlds in a single, piped expression; # The following is written as pseudocode # (don’t run it, just read it) # {assigning to bmi_sorted} <- # {start with the starwars table} %{then}% # {keep only the rows where species is “Human”} %{then}% # {create a new column called bmi using a calculation} %{then}% # {create a group for each unique value in homeworld} %{then}% # {for each group create a summary table (one col: avg_bmi)} %{then}% # {order the rows by decreasing avg_bmi value} # # {view bmi_sorted}
tibble()
with equal-length vectors to make a tibble.tibble( a = c(3, 5, 2, 6), b = c("a", "b", "g", "b") )
tibble()
with two vectors: one of length 1 and the other of length 4.tibble( a = 3, b = c("a", "b", "g", "b") )
tibble()
with two vectors that contain NA
values.tibble( a = c(3, 5, 2, NA), b = c("a", NA, "g", "b") )
NA
value in tibble()
.tibble( a = NA, b = c("a", "b", "g", "b") )
NA
value in tibble()
.tibble( a = NA_character_, b = c("a", "b", "g", "b") )
tribble()
function.tribble( ~a, ~b, 3, "a", 5, "b", 2, "g", 6, "b", )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.