I have the following example of dataset for a biology project. I want to compute a rate of growth of number between 4th January and 2nd of January. That is rate = (number_at_0104 - number_at_0102)/(number_at_0102) (In tidyverse if possible)
a <- c("Date", "Specie", "Number")
b <- c("2020-01-01", "Dog", "3")
c <- c("2020-01-02", "Dog", "4")
d <- c("2020-01-03", "Dog", "5")
e <- c("2020-01-04", "Dog", "6")
f <- c("2020-01-01", "Cat", "3")
g <- c("2020-01-02", "Cat", "7")
h <- c("2020-01-03", "Cat", "8")
i <- c("2020-01-04", "Cat", "10")
df <- as.data.frame(rbind(b, c, d, e, f, g, h, i))
names(df) <- a
df$Date <- as.Date(df$Date)
df$Number <- as.integer(df$Number)
I want to calculate a rate of growth. I know this has been treated already but I'm not sure whether I can apply it there. Usually, we use the lag() function but I have some questions.
- Can we tell the lag function what lags to use (eg not the previous periods but 4 periods before)
- My dataset is much bigger and for some specie (say cat) I want to compute the rate of growth between 20 Feb and 3 March. And for others (say dog) I want to compute between 5 May and 4th April. How can I do it?
Thank you in advance,
CodePudding user response:
using dplyr
start <- as.Date("2020-01-02")
end <- as.Date("2020-01-04")
df %>%
filter(Date 