Search code examples
rdplyraggregatena

Getting rid of NA values in R when trying to aggregate columns


df

enter image description here

I'm trying to aggregate this df by the last value in each corresponding country observation. For some reason, the last value that is added to the tibble is not correct.

aggre_data <- combined %>% 
    group_by(location) %>%
    summarise(Last_value_vacc = last(people_vaccinated_per_hundred)
aggre_data

I believe it has something to do with all of the NA values throughout the df. However I did try:

aggre_data <- combined %>% 
    group_by(location) %>%
    summarise(Last_value_vacc = last(people_vaccinated_per_hundred(na.rm = TRUE)))
aggre_data

Solution

  • Update:

    combined %>% 
        group_by(location) %>% 
        arrange(date, .by_group = TRUE) %>% # or whatever
        summarise(Last_value_vacc = last(na.omit( people_vaccinated_per_hundred)))