I'm a bit new at R, but I am trying to do something very simple using sapply because I will need to do it a lot. Say that you have many variables for 5 years, and want to divide the fifth row's values by the first row's for each of the columns at once.
a b c
184 20 55
100 32 563
18 12 88
5 99 52
32 36 22
So far I can either do it one by one:
df$a<-(df[5,]$a/df[1,]$a)
Or if I try to use sapply:
df2<-data.frame(sapply(names(df)[-1], function(x) {
(df[x]/df[x])
}))
The problem is that I don't know how to denote the rows with the sapply so above I'm just dividing vars by themselves. What's the quickest way of doing this? Thanks!