Reputation: 103
I have a large dataset that I would like to design the ability for individuals to select a single person for comparison and then to see how their data compares to the average of all of the other people. I'm trying to find the most efficient way possible and am hoping for some ideas.
I've tried creating two separate dataframes and then using lists to manually add the rows together so that I can graph them in ggplot. The extra manipulation, however also includes mutating the data.
library(tidyverse)
mtcars$car <- rownames(mtcars)
df <- mtcars %>%
filter(mtcars$car == 'Datsun 710') %>%
group_by(car) %>%
summarize(disp = first(disp))
df2 <- mtcars %>%
summarize(disp = mean(disp))
The expected output is as follows
group disp
Datsun 710 108
All cars 230.7219
Upvotes: 1
Views: 277
Reputation: 886938
With the output we got, it can be changed to
library(dplyr)
mtcars %>%
summarise(car = "All cars", disp = mean(disp)) %>%
bind_rows(df, .)
# A tibble: 2 x 2
# car disp
# <chr> <dbl>
#1 Datsun 710 108
#2 All cars 231.
It can also be done with add_row
. Create the column of row names with rownames_to_column
, select
the columns of interest, add the row with add_row
and filter
the rows based on the values of 'car'
library(tibble)
mtcars %>%
rownames_to_column('car') %>%
select(car, disp) %>%
add_row(car = "All cars", disp = mean(.$disp)) %>%
filter(car %in% c('Datsun 710', 'All cars'))
# car disp
#1 Datsun 710 108.0000
#2 All cars 230.7219
Upvotes: 1