1
votes

summary () ne reconnaît pas une variable

Je me demande pourquoi j'obtiens une Error: Problem with summarise() input wt_avg ci-dessous?

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`.


2 commentaires

HEPB_percent est un caractère "783%" par exemple. Vous avez également? 99% dans les données, ce qui pourrait être leur version de NA, je vérifierais la source


Vous devez convertir en numérique et effectuer un petit pré-traitement. as.numeric(str_remove_all(CA_vacc$HEPB_percent, "\\?|%"))


3 Réponses :


2
votes

Est-ce que ça marche:

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


0 commentaires

1
votes
 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)
)

0 commentaires

1
votes

Utilisation de la base R

with(CA_vacc, sum(as.numeric(gsub("[?%]", "", HEPB_percent)) * 
      ENROLLMENT, na.rm = TRUE)/sum(ENROLLMENT, na.rm = TRUE))
#[1] 96.76707


0 commentaires