 # Thread for Gov 1005/1006 Students to Submit a Practice Reprex

1 Like
``````students <- rep("M" = 14, "F" = 15)
#> Error in rep(M = 14, F = 15): attempt to replicate an object of type 'symbol'
``````

Created on 2020-02-12 by the reprex package (v0.3.0)

How do we use rep() again?

``````library(tidyverse)
library(moderndive)
library(reprex)

class <- tibble(id=1:30) %>%
mutate(gender = ifelse(id < 15, 0, 1)) %>%
select(gender)

sample <- rep_sample_n(class, size = 4, replace = FALSE, reps = 1000) %>%
group_by(replicate) %>%
summarize(total = sum(gender)) %>%
mutate(homogenous = ifelse((total == 0 | total == 4), 1, 0))

percentage_homogenous <-  sum(sample\$homogenous) / 10
``````

Created on 2020-02-12 by the reprex package (v0.3.0)

``````x <- data.frame(Gender = c("Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female", "Female"), "Number" = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
x
#>    Gender Number
#> 1    Male      1
#> 2    Male      1
#> 3    Male      1
#> 4    Male      1
#> 5    Male      1
#> 6    Male      1
#> 7    Male      1
#> 8    Male      1
#> 9    Male      1
#> 10   Male      1
#> 11   Male      1
#> 12   Male      1
#> 13   Male      1
#> 14   Male      1
#> 15 Female      0
#> 16 Female      0
#> 17 Female      0
#> 18 Female      0
#> 19 Female      0
#> 20 Female      0
#> 21 Female      0
#> 22 Female      0
#> 23 Female      0
#> 24 Female      0
#> 25 Female      0
#> 26 Female      0
#> 27 Female      0
#> 28 Female      0
#> 29 Female      0
#> 30 Female      0
``````

Created on 2020-02-12 by the reprex package (v0.3.0) ``````us_states <- map_data("state")
#> Error in map_data("state"): could not find function "map_data"

p <- ggplot(data = us_states,
aes(x = long, y = lat,
group = group, fill = region))
#> Error in ggplot(data = us_states, aes(x = long, y = lat, group = group, : could not find function "ggplot"

p + geom_polygon(color = "gray90", size = 0.1) + guides(fill = FALSE)
`````` ``````babynames %>% filter(names == "Carine") %>% count()
#> Error in babynames %>% filter(names == "Carine") %>% count(): could not find function "%>%"
``````
``````gov.1005.data::congress %>%
select(age, incumbent) %>%
filter(age)
#> Error in gov.1005.data::congress %>% select(age, incumbent) %>% filter(age): could not find function "%>%"
#> Error in gov.1005.data::congress %>% select(age, incumbent) %>% filter(age): could not find function "%>%"
``````

Created on 2020-02-12 by the reprex package (v0.3.0)

``````m = 14
f = 16
students <- c(rep(0,m), rep(1,f))

sample(students, replace = F)
#>   1 1 1 0 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 0 1 0 0 0 0 0 1 1 0 0
``````

Created on 2020-02-12 by the reprex package (v0.3.0) ``````library(tidyverse)
library(reprex)
library(ggthemes)

# 0 women
# 1 men

actual <- c(0, 2, 3, 0, 4, 3, 2, 2)

students <- rep(0, 4*length(actual) - sum(actual)) %>%
append(rep(1, sum(actual)))

results <- c()
trial <- c()

for (i in 1:500) {
for (f in 1:length(actual)) {
temp <- sample(students, 4, replace = FALSE)
trial[f] <- sum(temp, na.rm = TRUE)
}
results[i] <- var(trial)
trial <- c()
}

results_df <- as.data.frame(results)

actual_df <- as.data.frame(actual)

ggplot(results_df, aes(x = results)) +
geom_density(bindwidth = 1) +
geom_vline(xintercept = var(actual), linetype = "dotted") +
geom_text(aes("Variance = 2")) +
theme_economist()
#> Warning: Ignoring unknown parameters: bindwidth
#> Error: Discrete value supplied to continuous scale
`````` Created on 2020-02-12 by the reprex package (v0.3.0)

``````library(tidyverse)

v <- c(rep(1, 16), rep(0, 16))

students <- data.frame(number = c(rep(1:8, 4)),
gender = sample(v)) %>%
group_by(number) %>%
summarize(table = sum(gender)) %>%
mutate(clumped = ifelse(table %in% c(0, 4), TRUE, FALSE)) %>%
count(clumped)
``````

Created on 2020-02-12 by the reprex package (v0.3.0)

``````raw_deaths %>%

# selected relevant columns

select(census_region, gender, year, deaths, population, crude_rate, age_adjusted_rate) %>%
ggplot(aes(x = year, y = age_adjusted_rate, color = census_region)) +

# created line graph to show change over time

geom_line() +

# facetwrapped by gender to compare rates between males and females

facet_wrap(~gender) +

# added titles and other labels

labs(title = "Age-Adjusted Death Rates from Congenital Diseases By Region of the US",
subtitle = "From 1999 to 2016, the death rates have been decreasing, for all ages and races",
x = "Year",
caption = "Data from: Centers for Disease Control and Prevention",
color = "Region") +
scale_color_discrete(name = "Region", breaks=c("Census Region 1: Northeast", "Census Region 2: Midwest", "Census Region 3: South", "Census Region 4: West"), labels = c("Northeast", "Midwest", "South", "West"))
#> Error in raw_deaths %>% select(census_region, gender, year, deaths, population, : could not find function "%>%"
``````
``````num_even <- c()
for (i in 0:100){
sample_1 <- class %>%
mutate(seat_num = sample(1:30, replace = F)) %>%
mutate(table = ceiling(seat_num/ 4)) %>%
group_by(table) %>%
summarise(females = sum(gender_code))
num_even_split <- sample_1 %>%
filter(females == 2) %>%
count() %>%
pull()
num_even[i] <- num_even_split
i = i + 1
}
#> Error in class %>% mutate(seat_num = sample(1:30, replace = F)) %>% mutate(table = ceiling(seat_num/4)) %>% : could not find function "%>%"
num_even
#> NULL
mean(num_even)
#> Warning in mean.default(num_even): argument is not numeric or logical: returning
#> NA
#>  NA

ggplot() +
geom_histogram(aes(x = num_even))
#> Error in ggplot(): could not find function "ggplot"
`````` ``````babynames %>% filter(sex == "F") %>% count(name == "Alexandra")
#> Error in babynames %>% filter(sex == "F") %>% count(name == "Alexandra"): could not find function "%>%"
``````

Created on 2020-02-12 by the reprex package (v0.3.0)

``````x <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
table <- function(x) {
sample(x, size = 4)
}

ifelse(sum(table) == 4, TRUE, FALSE)
#> Error in sum(table): invalid 'type' (closure) of argument
``````

Created on 2020-02-12 by the reprex package (v0.3.0)

x <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
table <- sample(x, size = 4)

ifelse(sum(table) == 4, TRUE, FALSE)
#>  TRUE