Vedda
Vedda

Reputation: 7435

If rowSums greater than one, divide by sum

I'd like to divide by the sum of rows if the rowSums() is greater than one. I haven't thought of a way to do this without a for() loop, so I'm looking for a solution without a loop.

Sample Data

dat <- structure(list(x1 = c(0.18, 0, 0.11, 0.24, 0.33), x2 = c(0.34, 
0.14, 0.36, 0.35, 0.21), x3 = c(0.1, 0.36, 0.12, 0.07, 0.18), 
    x4 = c(0.08, 0.35, 0.06, 0.1, 0.09), x5 = c(0.26, 0.13, 0.22, 
    0.31, 0.22)), .Names = c("x1", "x2", "x3", "x4", "x5"), row.names = c(NA, 
5L), class = "data.frame")

> rowSums(dat)

   1    2    3    4    5 
0.96 0.98 0.87 1.07 1.03 

What I've tried

This works, but I wonder if there is a better way to do it:

a <- which(rowSums(dat) > 1)
dat[a, ] <- dat[a,] / rowSums(dat[a,]

> rowSums(dat)
   1    2    3    4    5 
0.96 0.98 0.87 1.00 1.00 

Upvotes: 2

Views: 295

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269491

This gives the same value as the expression near the end of the question:

dat / pmax(rowSums(dat), 1)

Upvotes: 8

lmo
lmo

Reputation: 38500

This is inferior to G. Grothendieck's answer, but you can also use ifelse.

rs <- rowSums(dat)
dat / ifelse(rs < 1, rs, 1L)
         x1        x2        x3         x4        x5
1 0.1875000 0.3541667 0.1041667 0.08333333 0.2708333
2 0.0000000 0.1428571 0.3673469 0.35714286 0.1326531
3 0.1264368 0.4137931 0.1379310 0.06896552 0.2528736
4 0.2400000 0.3500000 0.0700000 0.10000000 0.3100000
5 0.3300000 0.2100000 0.1800000 0.09000000 0.2200000

Upvotes: 4

Related Questions