Skip to content
Prev 305777 / 398506 Next

Conditional operations in R

Hello,

In R you would use vectorized instructions, not a do while loop.


dat <- read.table(text="
client   pct_total
A          15%
B          10%
C          10%
D          9%
E           8%
F          6%
G          4%
", header = TRUE)

# Make it numeric
dat$pct_total <- with(dat, as.numeric(sub("%", "", pct_total))/100)
str(dat)  # see its STRucture

top <- which(dat$pct_total >= median(dat$pct_total))  # make index vector
sum(dat$pct_total[top])

Hope this helps,

Rui Barradas
Em 18-09-2012 15:41, ramoss escreveu: