[R] Fastest Way to Divide Elements of Row With Its RowSum
William Revelle
lists at revelle.net
Thu Sep 17 08:15:37 CEST 2009
At 2:40 PM +0900 9/17/09, Gundala Viswanath wrote:
>I have a data frame (dat). What I want to do is for each row,
>divide each row with the sum of its row.
>
>The number of row can be large > 1million.
>Is there a faster way than doing it this way?
>
>datnorm;
>for (rw in 1:length(dat)) {
> tmp <- dat[rw,]/sum(dat[rw,])
> datnorm <- rbind(datnorm, tmp);
>}
>
>
>- G.V.
datnorm <- dat/rowSums(dat)
this will be faster if dat is a matrix rather than a data.frame.
Bill
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
William Revelle http://personality-project.org/revelle.html
Professor http://personality-project.org/personality.html
Department of Psychology http://www.wcas.northwestern.edu/psych/
Northwestern University http://www.northwestern.edu/
Use R for psychology http://personality-project.org/r
It is 5 minutes to midnight http://www.thebulletin.org
More information about the R-help
mailing list