Reputation: 7517
I'm wondering why I get Error: Problem with summarise() input wt_avg
below?
library(tidyverse)
CA_vacc <- read_csv('https://raw.githubusercontent.com/rnorouzian/e/master/2017-2018%20CA%20Vaccination%20Data.csv',
na = c(".","--*"))
CA_vacc %>% summarise(
wt_avg = sum(HEPB_percent * ENROLLMENT, na.rm = TRUE) / sum(ENROLLMENT, na.rm = TRUE)
)
# Error: Problem with `summarise()` input `wt_avg`.
Upvotes: 1
Views: 155
Reputation: 887048
Using base R
with(CA_vacc, sum(as.numeric(gsub("[?%]", "", HEPB_percent)) *
ENROLLMENT, na.rm = TRUE)/sum(ENROLLMENT, na.rm = TRUE))
#[1] 96.76707
Upvotes: 1
Reputation: 719
library(tidyverse)
CA_vacc %>%
mutate(HEPB_percent = as.numeric(str_remove_all(CA_vacc$HEPB_percent, "\\?|%"))) %>%
summarise(
wt_avg = sum(HEPB_percent * ENROLLMENT, na.rm = TRUE) / sum(ENROLLMENT, na.rm = TRUE)
)
Upvotes: 1
Reputation: 11584
Does this work:
library(dplyr)
library(readr)
CA_vacc %>% summarise(
wt_avg = sum(parse_number(HEPB_percent) * ENROLLMENT, na.rm = TRUE) / sum(ENROLLMENT, na.rm = TRUE)
+ )
# A tibble: 1 x 1
wt_avg
<dbl>
1 96.8
Upvotes: 2