1

Below is my data set.

Data set

To find mean for entire column (Column Name: BP) I have achieved it using the below R code

library(Sleuth3)
ex0112
View(ex0112)
mean(ex0112$BP)

But how do I calculate the mean (of BP) for only the Regular Oil Diet? I am new to R programming. Your help is highly appreciated

Roshan Singh
  • 45
  • 1
  • 6

5 Answers5

3

Maybe you can try the code below

with(ex0112,tapply(BP,Diet,mean))

If you are interested in RegularOil only

with(ex0112,tapply(BP,Diet,mean))["RegularOil"]
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1

Another option with dplyr

library(dplyr)
ex0112 %>%
     group_by(Diet) %>%
     summarise(BP = mean(BP))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

I would suggest a base R approach. Using some data to make an example, you can find the mean of BP. The aggregate() function allows a subset of data.

#Data
df <- structure(list(Diet = c("FishOil", "RegularOil", "FishOil", "RegularOil", 
"FishOil", "RegularOil", "FishOil", "RegularOil", "FishOil", 
"RegularOil", "FishOil", "RegularOil", "FishOil", "RegularOil"
), BP = c(0, 10, 2, 12, 13, -5, 5, 12, 5, 3, 13, 3, 8, 5)), class = "data.frame", row.names = c(NA, 
-14L))

The code:

#Aggregate
aggregate(BP~Diet,mean,data=df,subset = Diet=='RegularOil')

The output:

        Diet       BP
1 RegularOil 5.714286
Duck
  • 39,058
  • 13
  • 42
  • 84
1

You can try this with data.table using data from @Duck

library(data.table)
setDT(df)[, .(meanBP = mean(BP)), by = .(Diet)]
#         Diet   meanBP
# 1:    FishOil 6.571429
# 2: RegularOil 5.714286
Tho Vu
  • 1,304
  • 2
  • 8
  • 20
1

I would propose:

ex0112.dt <- as.data.table(ex0112)
ex0112.dt[,mean(BP),by=.(Diet)]

I ran in a few optimisation problems recently and data.table really saved me. that being said, benchmarking other solutions, tapply seems to be the winner :)

(bench = microbenchmark::microbenchmark(
  with(ex0112, tapply(BP,Diet,mean)),
  ex0112 %>%
    group_by(Diet) %>%
    summarise(BP = mean(BP), .groups = "drop_last"),
  aggregate(BP ~ Diet, ex0112, mean),
  ex0112.dt[,mean(BP),by=.(Diet)],
  times=1000L
))
ggplot2::autoplot(bench)

enter image description here

Will
  • 910
  • 7
  • 17