[R] Replace a for loop with a function

2011-09-19 Thread Eekhout, I.
Hi all, 

I would like to replace the for loop in the code below with a function
to improve the speed and to make the script more efficient. 
The loop creates a vector of integers (x) with the probability of f for
each integer.
The length of f is variable, but sums to 1. 
I tried to use a function with optional arguments which did not work.

Here is the code:

f - data.matrix(c(0.5,0.15,0.35))
u - runif(50) 
x - data.matrix(rep(1, n)) 
fc - 0
for(i in 1:length(f))   {
fc - fc + f[i] 
cf - ifelse(ufc,1,0)
x - x + cf
}
x

Can anyone help me with this translation?
Thanks in advance, 

Iris

__
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] Replace a for loop with a function

2011-09-19 Thread Eik Vettorazzi
Hi Iris,
maybe I misinterpret this, but I think in the end it all comes down to
sample(1:3,50,prob=c(0.5,0.15,0.35),replace=T))

cheers

Am 19.09.2011 17:16, schrieb Eekhout, I.:
 Hi all, 
 
 I would like to replace the for loop in the code below with a function
 to improve the speed and to make the script more efficient. 
 The loop creates a vector of integers (x) with the probability of f for
 each integer.
 The length of f is variable, but sums to 1. 
 I tried to use a function with optional arguments which did not work.
 
 Here is the code:
 
 f - data.matrix(c(0.5,0.15,0.35))
 u - runif(50) 
 x - data.matrix(rep(1, n)) 
 fc - 0
   for(i in 1:length(f))   {
   fc - fc + f[i] 
   cf - ifelse(ufc,1,0)
   x - x + cf
   }
 x
 
 Can anyone help me with this translation?
 Thanks in advance, 
 
 Iris
 
 __
 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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und 
Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Jörg F. Debatin (Vorsitzender), Dr. Alexander 
Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 

__
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] Replace a for loop with a function

2011-09-19 Thread Eik Vettorazzi
sorry, there is a superfluous bracket at the end of the line, it should
be read as
sample(1:3,50,prob=c(0.5,0.15,0.35),replace=T)

Am 19.09.2011 19:08, schrieb Eik Vettorazzi:
 Hi Iris,
 maybe I misinterpret this, but I think in the end it all comes down to
 sample(1:3,50,prob=c(0.5,0.15,0.35),replace=T))
 
 cheers
 
 Am 19.09.2011 17:16, schrieb Eekhout, I.:
 Hi all, 

 I would like to replace the for loop in the code below with a function
 to improve the speed and to make the script more efficient. 
 The loop creates a vector of integers (x) with the probability of f for
 each integer.
 The length of f is variable, but sums to 1. 
 I tried to use a function with optional arguments which did not work.

 Here is the code:

 f - data.matrix(c(0.5,0.15,0.35))
 u - runif(50) 
 x - data.matrix(rep(1, n)) 
 fc - 0
  for(i in 1:length(f))   {
  fc - fc + f[i] 
  cf - ifelse(ufc,1,0)
  x - x + cf
  }
 x

 Can anyone help me with this translation?
 Thanks in advance, 

 Iris

 __
 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.
 
 


-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und 
Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Jörg F. Debatin (Vorsitzender), Dr. Alexander 
Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 

__
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] Replace a for loop with a function

2011-09-19 Thread Jean V Adams
Eekhout, I. wrote on 09/19/2011 10:16:17 AM:
 
 Hi all, 
 
 I would like to replace the for loop in the code below with a function
 to improve the speed and to make the script more efficient. 
 The loop creates a vector of integers (x) with the probability of f for
 each integer.
 The length of f is variable, but sums to 1. 
 I tried to use a function with optional arguments which did not work.
 
 Here is the code:
 
 f - data.matrix(c(0.5,0.15,0.35))
 u - runif(50) 
 x - data.matrix(rep(1, n)) 
 fc - 0
for(i in 1:length(f))   {
   fc - fc + f[i] 
   cf - ifelse(ufc,1,0)
   x - x + cf
}
 x
 
 Can anyone help me with this translation?
 Thanks in advance, 
 
 Iris


There was no n in the sample code you shared.  I assumed n=50.

n - 50
f - data.matrix(c(0.5, 0.15, 0.35))
u - runif(n)

res - outer(u, cumsum(f), )
x - rowSums(res) + 1
x

Jean

[[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.