I have a dataframe as shown below listing the number of injuries by vehicle type:
trqldnum <- data.frame(motorveh=c(796,912,908,880,941,966,989,984),
I like the plyr
for these tasks as they allow you to specify the format of the output. You can turn this into a function that will scale to more columns and different base levels for the division easily.
FUN <- function(dat, baseRow = 1){
require(plyr)
divisors <- dat[baseRow ,]
adply(dat, 1, function(x) x / divisors)
}
FUN(trqldnum, 1)
motorveh motorcyc bicyc
1 1.000000 1.000000 1.000000
2 1.145729 1.147860 1.165138
3 1.140704 1.268482 1.146789
4 1.105528 1.217899 1.256881
5 1.182161 1.568093 1.577982
6 1.213568 1.513619 1.339450
7 1.242462 1.844358 1.587156
8 1.236181 1.929961 1.633028
How about
sweep(trqldnum,2,unlist(trqldnum[1,]),"/")
?
The unlist
is required to convert the first row of the data frame into a vector that can be swept ...
Convert the data-frame to a matrix and use matrix operations:
m <- as.matrix(trqldnum)
trqldprop <- as.data.frame( t(t(m)/m[1,]) )
> trqldprop
motorveh motorcyc bicyc
1 1.000000 1.000000 1.000000
2 1.145729 1.147860 1.165138
3 1.140704 1.268482 1.146789
4 1.105528 1.217899 1.256881
5 1.182161 1.568093 1.577982
6 1.213568 1.513619 1.339450
7 1.242462 1.844358 1.587156
8 1.236181 1.929961 1.633028
Note that we need to transpose the matrix (see the t(m)
) because when you divide a matrix by a vector, the operation is done column-wise.
Some version of Prasad solution without conversion to matrix.
trqldnum/trqldnum[1,][rep(1,nrow(trqldnum)),]