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 bindata } 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? Thanks, Hedderik. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Look at the help for the cut() function, which does what you want, I think. Kjetil Halvorsen rijn at swi.psy.uva.nl wrote:> > 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 > > bindata > } > > 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? > > Thanks, > Hedderik. > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, thanks to all who responded to my question. The various responses teached me, again, a lot about R and the different philosophy of R compared to programming languages. Especially the reply of Peter Malewski using the tapply/unlist/rep functions was very insightful, and helped me to solve my problems. To explain why I needed this all: in a lexical decision (psychonomy) experiment, the subjects had to respond at several predefined lags. However, the response times show quite some variation, so besides an analysis based on the 6 predefined lags, we also wanted to do an analysis based on 6 groups each containing 1/6th of the fastest, slowest, etc RTs per subject. So, in the orginial dataframe, we had the columns subject, lag and rt, and the code I posted in my last message was meant to add the column binlag. subject lag rt binlag 1 1 350 1 1 1 450 2 1 2 425 1 1 2 475 2 2 1 425 1 2 1 450 1 2 2 490 2 2 2 480 2 . . Sorry if I was unclear in my previous postings. Again, thanks for all the help! Hedderik. P.S. The code I use now, is: bin.index <- function(data,bins=2) { # Code from Peter Malewski, added the sort command. sort(rep(rep(1:bins,rep(length(data)/bins,bins)),length.out=length(data))) } cat("Binning...") stor <- stor[order(stor$subject,stor$rt),] stor$binlag <- unlist(tapply(stor$rt,stor$subject,bin.index,bins=6)) print("done") -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._