this is a simple question, but I am not proficient in automation in R. So I'm asking this.
I have a dataframe like this:
x<-c(2,5,1,1,23,1,23,51,36,85,24,1,645,945,
2,8,124,4,35,6,71,45,1,5,12,52,764,8,6,234,
98,234,456,6,1,452,32,176,75,31)
x<-matrix(x,ncol=5)
x<-data.frame(x)
names(x)<-c("car","bike","bus","train","walk")
so in the end you'd get a dataframe like this:
car bike bus train walk
2 36 124 12 456
5 85 4 52 6
1 24 35 764 1
1 1 6 8 452
23 645 71 6 32
1 945 45 234 176
23 2 1 98 75
51 8 5 234 31
So I want to calculate the growth of each transportation:
x$car_growth<-((diff(x$car)/x$car)*100)
however, the first value will be empty, so I need to arrange it such that it will go into the right row. To do that, I make a new df
k<-data.frame(seq(from=1,to=8)
names(k)<-c("seq")
k2<-data.frame(x$car_growth)
k2$seq<-seq(from=2, to=9)
k3<-merge(k2, k, by="seq", all=TRUE)
k3<-k3[1:8,]
x2<-cbind(x,k3)
x2$seq<-NULL
So in the end you will get something like this
car bike bus train walk car_growth
2 36 124 12 456 NA
5 85 4 52 6 150
1 24 35 764 1 -400
1 1 6 8 452 0
23 645 71 6 32 2200
1 945 45 234 176 -2200
23 2 1 98 75 2200
51 8 5 234 31 48.7
These are only 1 process for item car.
I wanna repeat this for bike, bus, train, and walk.
Of course the original data that I have is MUCHHH bigger and longer than this. I just want to learn how to automate all these processes into a few lines short script like excel VBA
Thanks for all your suggestions.