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(u>fc,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.


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(u>fc,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 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(u>fc,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.


[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(u>fc,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

2010-02-10 Thread Ivan Calandra
Well actually, in that case, I don't think it is really necessary to 
replace the loop because it is quite fast with the small size I have. 
But I thought it would help me to understand how would vectorization 
work and how to use the *apply functions.

Thanks anyway for your piece of advice!
Ivan

Le 2/10/2010 17:03, Petr PIKAL a écrit :

Hi

how many objects do you have in a list? Loop is ineffective if you use
several nested loops and/or there is some unnecessary mimic of simple
function.

e.g. you can use this

set.seed(666)
x<- runif(10)
vysled<- 0
for(i in 1:length(x)) {
if(vysled>  x[i]) vysled<- vysled else vysled<- x[i]
}
vysled

but I would prefer max(x)

If you do not perceive performance issues there is usually no need to
elaborate *apply.

Regards
Petr



r-help-boun...@r-project.org napsal dne 10.02.2010 11:59:21:

   

After reading the R news, I've tried this code and it works:
  >  rapply(list(names(test),test), write.csv, file="filename.csv",
append=T, row.names=F)

However, the output is structured like this:
names(test)[[1]]
names(test)[[2]]
etc...
test[[1]]
test[[2]]
etc...

I would like to alternate names(test) and test in the output. The
desired output would be structured like this:
names(test)[[1]]
test[[1]]
names(test)[[2]]
test[[2]]
etc...

Can someone guide me for that step? Better solutions for the whole thing
 
   

are of course welcomed!

Thanks a lot
Ivan

Le 2/10/2010 10:50, Ivan Calandra a écrit :
 

Hi everybody!

I'm still quite new in R and I don't really understand this whole
"vectorization" thing.
For now, I use a for loop (and it works fine), but I think it would be
   
   

useful to replace it.

I want to export the result of a test statistic, which is stored as a
list, into a csv file. I therefore have to export each element of the
list separately.
Here is the code:

str(test)
List of 3
  $ output : num [1:15, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
   ..- attr(*, "dimnames")=List of 2
   .. ..$ : NULL
   .. ..$ : chr [1:6] "con.num" "psihat" "p.value" "p.crit" ...
  $ con: num [1:6, 1:15] 1 -1 0 0 0 0 1 0 -1 0 ...
  $ num.sig: int 0

for (i in 1:3){
  write.csv(test[[i]], file="filename.csv", append=T, quote=F,
row.names=T)
}


As I said, I don't completely understand, but I think one of these
"apply" function might do what I need.

Thanks in advance
Ivan

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





__
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

2010-02-10 Thread Petr PIKAL
Hi

how many objects do you have in a list? Loop is ineffective if you use 
several nested loops and/or there is some unnecessary mimic of simple 
function.

e.g. you can use this

set.seed(666)
x <- runif(10)
vysled <- 0
for(i in 1:length(x)) {
if(vysled > x[i]) vysled <- vysled else vysled <- x[i]
}
vysled

but I would prefer max(x)

If you do not perceive performance issues there is usually no need to 
elaborate *apply.

Regards
Petr



r-help-boun...@r-project.org napsal dne 10.02.2010 11:59:21:

> After reading the R news, I've tried this code and it works:
>  > rapply(list(names(test),test), write.csv, file="filename.csv", 
> append=T, row.names=F)
> 
> However, the output is structured like this:
> names(test)[[1]]
> names(test)[[2]]
> etc...
> test[[1]]
> test[[2]]
> etc...
> 
> I would like to alternate names(test) and test in the output. The 
> desired output would be structured like this:
> names(test)[[1]]
> test[[1]]
> names(test)[[2]]
> test[[2]]
> etc...
> 
> Can someone guide me for that step? Better solutions for the whole thing 

> are of course welcomed!
> 
> Thanks a lot
> Ivan
> 
> Le 2/10/2010 10:50, Ivan Calandra a écrit :
> > Hi everybody!
> >
> > I'm still quite new in R and I don't really understand this whole 
> > "vectorization" thing.
> > For now, I use a for loop (and it works fine), but I think it would be 

> > useful to replace it.
> >
> > I want to export the result of a test statistic, which is stored as a 
> > list, into a csv file. I therefore have to export each element of the 
> > list separately.
> > Here is the code:
> > 
> > str(test)
> > List of 3
> >  $ output : num [1:15, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
> >   ..- attr(*, "dimnames")=List of 2
> >   .. ..$ : NULL
> >   .. ..$ : chr [1:6] "con.num" "psihat" "p.value" "p.crit" ...
> >  $ con: num [1:6, 1:15] 1 -1 0 0 0 0 1 0 -1 0 ...
> >  $ num.sig: int 0
> >
> > for (i in 1:3){
> >  write.csv(test[[i]], file="filename.csv", append=T, quote=F, 
> > row.names=T)
> > }
> > 
> >
> > As I said, I don't completely understand, but I think one of these 
> > "apply" function might do what I need.
> >
> > Thanks in advance
> > Ivan
> >
> > __
> > 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.

__
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

2010-02-10 Thread Ivan Calandra

After reading the R news, I've tried this code and it works:
> rapply(list(names(test),test), write.csv, file="filename.csv", 
append=T, row.names=F)


However, the output is structured like this:
names(test)[[1]]
names(test)[[2]]
etc...
test[[1]]
test[[2]]
etc...

I would like to alternate names(test) and test in the output. The 
desired output would be structured like this:

names(test)[[1]]
test[[1]]
names(test)[[2]]
test[[2]]
etc...

Can someone guide me for that step? Better solutions for the whole thing 
are of course welcomed!


Thanks a lot
Ivan

Le 2/10/2010 10:50, Ivan Calandra a écrit :

Hi everybody!

I'm still quite new in R and I don't really understand this whole 
"vectorization" thing.
For now, I use a for loop (and it works fine), but I think it would be 
useful to replace it.


I want to export the result of a test statistic, which is stored as a 
list, into a csv file. I therefore have to export each element of the 
list separately.

Here is the code:

str(test)
List of 3
 $ output : num [1:15, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:6] "con.num" "psihat" "p.value" "p.crit" ...
 $ con: num [1:6, 1:15] 1 -1 0 0 0 0 1 0 -1 0 ...
 $ num.sig: int 0

for (i in 1:3){
 write.csv(test[[i]], file="filename.csv", append=T, quote=F, 
row.names=T)

}


As I said, I don't completely understand, but I think one of these 
"apply" function might do what I need.


Thanks in advance
Ivan

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


Re: [R] replace a for loop

2010-02-10 Thread Liviu Andronic
On Wed, Feb 10, 2010 at 9:50 AM, Ivan Calandra
 wrote:
> I'm still quite new in R and I don't really understand this whole
> "vectorization" thing.
>
See [1] for an article on vectorisation and loops in R.
Liviu

[1] http://www.r-project.org/doc/Rnews/Rnews_2008-1.pdf

__
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] replace a for loop

2010-02-10 Thread Ivan Calandra

Hi everybody!

I'm still quite new in R and I don't really understand this whole 
"vectorization" thing.
For now, I use a for loop (and it works fine), but I think it would be 
useful to replace it.


I want to export the result of a test statistic, which is stored as a 
list, into a csv file. I therefore have to export each element of the 
list separately.

Here is the code:

str(test)
List of 3
 $ output : num [1:15, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:6] "con.num" "psihat" "p.value" "p.crit" ...
 $ con: num [1:6, 1:15] 1 -1 0 0 0 0 1 0 -1 0 ...
 $ num.sig: int 0

for (i in 1:3){
 write.csv(test[[i]], file="filename.csv", append=T, quote=F, row.names=T)
}


As I said, I don't completely understand, but I think one of these 
"apply" function might do what I need.


Thanks in advance
Ivan

__
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 lapply or relative

2010-02-04 Thread jim holtman
Have you considered using a different data structure:

> # change the data structure
> x <- data.frame(
+ type=rep(c('x1', 'x2', 'x3'), each=100),
+ high=c(d[,4], d[,5], d[,6]),
+ value=c(d[,1], d[,2], d[,3]))
> head(x)
  type high  value
1   x10  0.8936737
2   x10 -1.0472981
3   x11  1.9713374
4   x10 -0.3836321
5   x11  1.6541453
6   x11  1.5122127
> sapply(split(x$value, list(x$high, x$type)), function(a) c(min=min(a), 
> max=max(a)))
 0.x1 1.x10.x2  1.x2   0.x3  1.x3
min -2.592328 1.364435 0.001314657 0.9447914 -2.9324797 0.9427577
max  1.324004 1.971337 0.925323476 0.9906600  0.8767704 1.5358077
>
>
>


On Thu, Feb 4, 2010 at 4:40 PM, David Freedman <3.14da...@gmail.com> wrote:
>
> Dear helpers.
> I often need to make dichotomous variables out of continuous ones (yes, I
> realize the problems with throwing away much of the information), but I then
> like to check the min and max of each category.  I have the following simple
> code to do this that cuts each variable (x1,x2,x3) at the 90th percentile,
> and then prints the min and max of each category:
>
> d=data.frame(x1=rnorm(100),x2=runif(100)); d=transform(d,x3=x1-x2)
> d[,4:6]=data.frame(sapply(d,function(v)as.numeric(v>=quantile(v,0.9;
> names(d)[4:6]=c('x1high','x2high','x3high')
> head(d)
> for (i in
> 1:3){print(do.call(rbind,by(d[,i],d[,i+3],function(x)(c(min(x),max(x))}
>
> Is there a way to replace the ugly for loop in the last line with some type
> of apply function that would know that my continuous and indicator variable
> are 3 variables apart in the dataframe?
>
> Thanks very much
> David Freedman
> --
> View this message in context: 
> http://n4.nabble.com/replace-a-for-loop-with-lapply-or-relative-tp1469453p1469453.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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] replace a for loop with lapply or relative

2010-02-04 Thread David Freedman

Dear helpers.
I often need to make dichotomous variables out of continuous ones (yes, I
realize the problems with throwing away much of the information), but I then
like to check the min and max of each category.  I have the following simple
code to do this that cuts each variable (x1,x2,x3) at the 90th percentile,
and then prints the min and max of each category:

d=data.frame(x1=rnorm(100),x2=runif(100)); d=transform(d,x3=x1-x2)
d[,4:6]=data.frame(sapply(d,function(v)as.numeric(v>=quantile(v,0.9;
names(d)[4:6]=c('x1high','x2high','x3high')
head(d)
for (i in
1:3){print(do.call(rbind,by(d[,i],d[,i+3],function(x)(c(min(x),max(x))}

Is there a way to replace the ugly for loop in the last line with some type
of apply function that would know that my continuous and indicator variable
are 3 variables apart in the dataframe?

Thanks very much
David Freedman
-- 
View this message in context: 
http://n4.nabble.com/replace-a-for-loop-with-lapply-or-relative-tp1469453p1469453.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.