[R] Loop removal?
Peter Malewski
p.malewski at tu-bs.de
Sun Jul 30 12:39:22 CEST 2000
I don't know if I understand you right, but..
> xx <- function(x,g=2)rep(1:g,rep(length(x) / g , g ) )
> zz <- data.frame (data=rnorm(100), id=rep(1:10,10))
> cbind(zz,nn=unlist( tapply(zz$data,zz$id,function(x)xx(x[order(x)] ,
g=2) )))[1:13,]
data id nn
1 -1.06975967 1 1
2 1.30402032 2 1
3 -0.08553785 3 1
4 0.11884050 4 1
5 -0.10690368 5 1
6 1.13232754 6 2
7 1.97065424 7 2
8 -0.19009641 8 2
9 -0.72320338 9 2
10 -0.53456589 10 2
11 -0.01734236 1 1
12 0.04074360 2 1
13 2.28373113 3 1
>
BUT NOTE, that this only works if
length(x) %% g == 0
...
Peter
On Sun, 30 Jul 2000 rijn at swi.psy.uva.nl wrote:
> Hi,
>
> > Look at the help for the cut() function, which does what you want,
> > I think.
>
> Thanks for the suggestion. I'm sure I can put it to good use in another
> part of the analysis, but it does not exactly what I want for this problem
> (as far as I understand). Given the following example:
>
> x <- c(1,1,2,3,4,4,3,2,1,1)
>
> And assuming I want to divide x into to equally long sets to which the
> values get assigned in increasing order, cut doesn't do the job:
>
> > cut(x,c(1,2,4),labels=F,include.lowest=T)
> [1] 1 1 1 2 2 2 2 1 1 1
> > table(cut(x,c(1,2,4),labels=F,include.lowest=T))
>
> 1 2
> 6 4
> >
>
> Whereas I'm looking for an output that results in:
>
> > table(...(x,...))
>
> 1 2
> 5 5
> >
>
> So, the assignment of factors is not only based on the size of the numbers
> in the vector, but also on the lengths of the resulting vectors.
>
> And, it still leaves this "loop"-problem:
>
> > > for (i in unique(stor$subject)) {
> > > stor[stor$subject == i,]$bin <- bindata(stor[stor$subject == i,]$rt)
> > > }
>
> I assume I should use a function like tapply, but:
>
> > bindata2 <- function(rt) {
> +
> + bindata <- rep(-1,length(rt))
> + binwidth <- length(rt)/2
> +
> + bindata[order(rt)[(0*binwidth)+1:(1*binwidth)]] <- 1
> + bindata[order(rt)[(1*binwidth)+1:(2*binwidth)]] <- 2
> +
> + bindata
> + }
> > x <- data.frame(subject=c(1,1,1,2,2,2),rt=c(1,2,3,3,2,4))
> > tapply(x$rt,x$subject,bindata2)
> $"1"
> [1] 1 2 2
>
> $"2"
> [1] 2 1 2
>
> the problem is that I don't know how to combine/concatenate the output of
> tapply into one vector again. So, it's probably all caused by a very
> simple "lack of knowledge about R" on my side...
>
> Right now, I use tapply, but use a for-loop again to concatenate the list
> into a single vector again. But it feels like there should be a better
> solution. I someone can hint me towards one, I would be very pleased.
>
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
**************************************************************************
P.Malewski Tel.: 0531 500965
Maschplatz 8 mailto: Peter.Malewski at gmx.de
************************38114 Braunschweig********************************
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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