[R] Loop removal?

rijn@swi.psy.uva.nl rijn at swi.psy.uva.nl
Fri Jul 28 20:21:21 CEST 2000

Dear all,

I've got a quite large dataframe (stor) with rows subject and rt (reaction
time). I would like to split the reaction times per subject into 6 bins of
equal size. Right now, I'm using the following code:

bindata <- function(rt) {

  bindata <- rep(-1,length(rt))
  binwidth <- length(rt)/6

  bindata[order(rt)[(0*binwidth)+1:(1*binwidth)]] <- 1
  bindata[order(rt)[(1*binwidth)+1:(2*binwidth)]] <- 2
  bindata[order(rt)[(2*binwidth)+1:(3*binwidth)]] <- 3
  bindata[order(rt)[(3*binwidth)+1:(4*binwidth)]] <- 4
  bindata[order(rt)[(4*binwidth)+1:(5*binwidth)]] <- 5
  bindata[order(rt)[(5*binwidth)+1:(6*binwidth)]] <- 6


for (i in unique(stor$subject)) {
  stor[stor$subject == i,]$bin <- bindata(stor[stor$subject == i,]$rt)

Although this code works fine, it does not "feel" very R'ish. Is there a
more elegant solution for this, especially one that removes the for-loop?


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list