Home > database >  Adding CI columns to dataframe
Adding CI columns to dataframe

Time:06-21

I have this dataframe:

> print(merged)
    AgeGroup    values                ind
1          1 0.2449762 diff_v.ownhigh_avg
2          1 0.2598964 diff_v.ownhigh_avg
3          1 0.2519043 diff_v.ownhigh_avg
4          1 0.2452479 diff_v.ownhigh_avg
5          1 0.2840650 diff_v.ownhigh_avg
6          1 0.2589341 diff_v.ownhigh_avg
7          1 0.3201843 diff_v.ownhigh_avg
8          1 0.3218865 diff_v.ownhigh_avg
9          1 0.2822984 diff_v.ownhigh_avg
10         1 0.3313962 diff_v.ownhigh_avg

I'd like to add two columns: One for the upper bound CI, one for the lower bound CI based on the means of the values column.

There are 8 different types of ind, and 2 age groups - the data would need to be grouped by these.

I'd like my output to look a bit like this ideally:

> print(merged)
    AgeGroup    values                ind   Upper_Bound     Lower_Bound
1          1 0.2449762 diff_v.ownhigh_avg   0.222       0.444
2          1 0.2598964 diff_v.ownhigh_avg   0.222       0.444
3          1 0.2519043 diff_v.ownhigh_avg   0.222       0.444
4          1 0.2452479 diff_v.ownhigh_avg   0.222       0.444
5          1 0.2840650 diff_v.ownhigh_avg   0.222       0.444
6          1 0.2589341 diff_v.ownhigh_avg   0.222       0.444
7          1 0.3201843 diff_v.ownhigh_avg   0.222       0.444
8          1 0.3218865 diff_v.ownhigh_avg   0.222       0.444
9          1 0.2822984 diff_v.ownhigh_avg   0.222       0.444
10         1 0.3313962 diff_v.ownhigh_avg   0.222       0.444

CodePudding user response:

You may try

library(dplyr)
df %>%
  group_by(AgeGroup) %>%
  mutate(upper = mean(values)   1.96 * sd(values),
         lower = mean(values) - 1.96 * sd(values))


   AgeGroup values ind                upper lower
      <int>  <dbl> <chr>              <dbl> <dbl>
 1        1  0.245 diff_v.ownhigh_avg 0.346 0.214
 2        1  0.260 diff_v.ownhigh_avg 0.346 0.214
 3        1  0.252 diff_v.ownhigh_avg 0.346 0.214
 4        1  0.245 diff_v.ownhigh_avg 0.346 0.214
 5        1  0.284 diff_v.ownhigh_avg 0.346 0.214
 6        1  0.259 diff_v.ownhigh_avg 0.346 0.214
 7        1  0.320 diff_v.ownhigh_avg 0.346 0.214
 8        1  0.322 diff_v.ownhigh_avg 0.346 0.214
 9        1  0.282 diff_v.ownhigh_avg 0.346 0.214
10        1  0.331 diff_v.ownhigh_avg 0.346 0.214
  • Related