My (made-up) data:
dat <- structure(list(animal = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"),
oxygen = c(25L, 24L, 28L, 30L, 25L, 30L, 28L, 27L, 20L, 22L,
20L, 27L, 26L, 24L, 26L, 22L, 30L, 25L, 26L, 28L, 27L, 30L,
27L, 28L, 28L, 20L, 23L, 29L), time = c(49L, 33L, 2L, 22L,
15L, 22L, 49L, 40L, 11L, 2L, 24L, 48L, 32L, 18L, 39L, 46L,
6L, 24L, 26L, 40L, 26L, 26L, 1L, 36L, 4L, 17L, 50L, 24L),
habitat = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L), .Label = c("clean", "dirty"), class = "factor")), .Names = c("animal",
"oxygen", "time", "habitat"), class = "data.frame", row.names = c(NA,
-28L))
Variable explanations:
animal: There are 4 individual animals (A, B, C, and D) tested for oxygen consumption.
oxygen: Oxygen consumption rate; each animal was measured multiple times.
time: The time (measured in minute) since a machine started to measure oxygen consumption.
condition: ndicates habitat conditions from which an animal was collected; clean or dirty (polluted) habitat.
What I want to test (by a t-test
) is where the mean oxygen consumption rates differ between animals from clean and dirty (polluted) habitats. But, I want to restrict my analysis to the lowest one-third of oxygen consumption values for each animal taken between 5 to 48 minutes.
Could anyone please provide me R codes that can subset my data to contain only the lowest one-third of the oxygen consumption rates for each animal AND the rates taken between 5-48 minutes?
I am trying something like this, but the following code does not do what I want (what it does, I think, is that it selects the lowest one-third from ALL data, not the lowest-one third for each animal):
newdat <- subset(dat, oxygen <= quantile(oxygen, 1/3) & time >= 5 & time >=48)