Re: [R] Counter in a For Loop - Efficiency Issue

2011-02-02 Thread Bart Joosen

without a reproducible sample, it is hard to tell, but I will give it a shot.
Maybe it's possible to merge your M with M2: merge(M, M2)
If you only want to count times, you can use seq_along(x) in a by function
eg:
dat <- data.frame(person=rep(c(1,2,3), each=5), time=rnorm(15))
by(dat$person, dat$person, function(x) seq_along(x))

Ofcourse you can use any of the other vectorisation functions (lapply,
apply, aggregate, )

HTH


Bart
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Counter-in-a-For-Loop-Efficiency-Issue-tp3253674p3253698.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Counter in a For Loop - Efficiency Issue

2011-02-02 Thread Petr Savicky
On Wed, Feb 02, 2011 at 09:35:28AM +0200, Leendert Punt wrote:
> I have a 32000 x 14 matrix (M) where entry (x, y) corresponds to
> person x at a discrete time y. I then have a matrix (M2) of 6 x 2
> where each entry is a an event by a person that is in the above 32000
> and at a time that is in the range of the discrete time points above.
> I want to populate the another matrix (M3) such that (x, y) is the
> number of events of person x between times y-1 and y. This is simple
> to code, (I just put the discrete time points and the 32000 people in
> their own vectors, V1 and V2):
> 
> 
> for (i in 1:32000){
> for (j in 1:6){
> 
> for (k in 2:14){
> 
> if (M2[1, j] == V2[i]   &   M2[2, j]  > V1[k-1]   &  M2[2, j] <= V1[k])
> M3[i, k] = M3[i, k]+1
> 
> }}}

Consider the following approach, which uses M2, V1 and V2 as
used in the code above.

  V1 <- c(0, 10, 20, 30, 40)
  V2 <- 1:3 # list of persons in the required order
  M2 <- rbind(
  c(1, 2, 1, 1, 3, 1, 3, 1),
  c(11, 21, 21, 21, 11, 21, 11, 31))

  person <- factor(M2[1, ], levels=V2)
  interval <- cut(M2[2, ], breaks=V1)
  M3 <- table(person, interval)

  M2

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]12113131
[2,]   11   21   21   21   11   21   11   31

  M3

interval
  person (0,10] (10,20] (20,30] (30,40]
   1  0   1   3   1
   2  0   0   1   0
   3  0   2   0   0

Does this approach work for your data?

Petr Savicky.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Counter

2009-07-23 Thread Jorge Ivan Velez
Try also:
t( apply(x, 1, function(x) table( factor(x, levels = 0:2) ) ) )
#   0 1 2
# [1,] 1 2 1
# [2,] 1 3 0
# [3,] 3 0 1
# [4,] 0 2 2

See ?apply, ?table and ?factor for more details, examples and information.

HTH,

Jorge


On Thu, Jul 23, 2009 at 6:44 AM, amor Gandhi  wrote:

> Hi everyone,
>
> Is there any counter function in R for the following purpose:
> x <- matrix(c(1,1,0,2,1,0,0,2,0,1,2,1,2,1,0,1),nrow=4)
> As I would like to know how many zeros, ones, and twos in each row of x?
>
> Many thank in advance,
> Amor
>
>
>
>
>
>[[alternative HTML version deleted]]
>
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Counter

2009-07-23 Thread Philipp Pagel
On Thu, Jul 23, 2009 at 10:44:16AM +, amor Gandhi wrote:
> Hi everyone,
>
> Is there any counter function in R for the following purpose:
> x <- matrix(c(1,1,0,2,1,0,0,2,0,1,2,1,2,1,0,1),nrow=4)

> As I would like to know how many zeros, ones, and twos in each row of x?

Yes there is:

As you are interested int he counts by row you can use apply:

apply(x, 1, table)

cu
Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://webclu.bio.wzw.tum.de/~pagel/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Counter

2009-07-23 Thread Uwe Ligges

apply(x, 1, table)

Uwe Ligges


amor Gandhi wrote:

Hi everyone,
 
Is there any counter function in R for the following purpose:

x <- matrix(c(1,1,0,2,1,0,0,2,0,1,2,1,2,1,0,1),nrow=4)
As I would like to know how many zeros, ones, and twos in each row of x?
 
Many thank in advance,

Amor
 
 



  
	[[alternative HTML version deleted]]






__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.