Like the title says, when I run the code inside the R markdown file everything works fine and I do not get any errors but when I try to Knit I get an error.
This is the code chunk where it says the first error is happening (this failing is obviously causing a whole chain of other errors afterwards because dta1 is not being created).
dta1 <- df %>%
group_by(z_assignment) %>%
summarise(Arrest = count(t_final == 1),
Advise = count(t_final == 2),
Separate = count(t_final == 3),
Total = sum(Arrest, Advise, Separate)) %>%
arrange(Separate) %>%
bind_rows(summarise_all(., funs(if(is.numeric(.)) sum(.) else "Total")))
the error I am getting is this
## Error: Problem with `summarise()` input `Arrest`.
## x no applicable method for 'group_vars' applied to an object of class "logical"
## i Input `Arrest` is `count(t_final == 1)`.
## i The error occurred in group 1: z_assignment = "advise".
These are the libraries I am using (I have them in my setup chunk)
library(tidyverse)
library(AER)
library(stargazer)
library(haven)
library(lfe)
library(estimatr)
library(stringr)
library(dplyr)
library(modelsummary)
library(ggplot2)
library(haven)
and this is the data I am using (this is the only code chunk above the one causing the error)
df <- read_dta("http://masteringmetrics.com/wp-content/uploads/2015/02/mdve.dta")
df <- df %>%
rename_all(tolower) %>%
mutate(z_assignment = case_when(
t_random == 1 ~ "arrest",
t_random == 2 ~ "advise",
t_random == 3 ~ "separate"
),
d_actual = case_when(
t_final == 1 ~ "arrest",
t_final == 2 ~ "advise",
t_final == 3 ~ "separate",
t_final == 4 ~ "other"
),
z_coddled = ifelse(z_assignment %in% c("separate", "advise"),1,0),
d_coddled = ifelse(d_actual %in% c("separate", "advise"),1,0)
) %>%
filter(d_actual != "other")
df <- df %>%
mutate( pz_separ=1/(1+exp(1.21)),
pz_arrest=1/(1+exp(1.21+.9)),
pz_advise=1/(1+exp(1.21+.21)),
pd_separ=1/(1+exp(1.05)),
pd_arrest=1/(1+exp(1.05+.82)),
pd_advise=1/(1+exp(1.05+.46)),
order = row_number()/n()) %>%
group_by(t_random) %>%
mutate(z_rank = row_number()/n()) %>%
ungroup() %>%
mutate(Y = ifelse( (z_assignment == "arrest" & z_rank < pz_arrest) |
(z_assignment == "advise" & z_rank < pz_advise) |
(z_assignment == "separate" & z_rank < pz_separ), 1,0)) %>%
select(t_random, z_assignment, t_final, d_actual, z_coddled, d_coddled, Y, v_race, s_race, weapon, s_chem, year, month)
So what am I doing wrong/missing?
The issue is with count
which expects a tibble/data.frame
as input and not a column/vector. It should be sum
library(dplyr)
df %>%
group_by(z_assignment) %>%
summarise(Arrest = sum(t_final == 1),
Advise = sum(t_final == 2),
Separate = sum(t_final == 3),
Total = sum(Arrest, Advise, Separate)) %>%
arrange(Separate) %>%
bind_rows(summarise_all(., funs(if(is.numeric(.)) sum(.) else "Total")))
-output
# A tibble: 4 x 5
# z_assignment Arrest Advise Separate Total
# <chr> <int> <int> <int> <int>
#1 arrest 91 0 1 92
#2 advise 19 84 5 108
#3 separate 26 5 83 114
#4 Total 136 89 89 314
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments