3
I’m trying to create an index number from a data frame. I would like to divide each column by the first number without having to do the division column by column.
Just as an example, df would be:
DF1 <- data.frame(Name = c("Aaron", "Blake"), Jan = c(2, 5), Feb = c(4, 6), Mar = c(3, 4))
Making column by column, for example:
library(tidyverse)
DF1$Fev/first(DF1$Fev)*100
the result is expected:
[1] 100 150
But doing
DF1[,-1]/first(DF1[,-1])*100
get
Jan Fev Mar
1 100 200 150
2 100 120 80
The appropriate would be
Name Jan Fev Mar
1 Aaron 100 100 100.0000
2 Blake 250 150 133.3333
Can anyone help me? I will be working with a data frame with many columns soon.