Re: [R] loop in list

2012-07-01 Thread Rui Barradas

Hello,

Sorry, forgot to Cc the list.

Em 01-07-2012 01:24, R. Michael Weylandt  
escreveu:

I might think replicate() is slightly more idiomatic, but I'm not in a position 
to check if simplify=FALSE will keep a list.



It does:

class(replicate(20, f(1)))  # "matrix"
class(replicate(20, f(1), simplify=FALSE))  # "list"

Rui Barradas


Best,
Michael

On Jun 30, 2012, at 7:13 PM, Rui Barradas  wrote:


Hello,

You can avoid the loop using lapply.

f <- function(x) sample(100, 10)
samp.list <- lapply(1:20, f)

will choose 20 samples of 10 integers up to 100 and put them in a list. All you 
need is to write a function f(). f() must have an argument, even if it doesn't 
use it. If you need other arguments to be processed by f(), define it and call 
it as, for instance using the example above,

f <- function(x, ...) sample(100, 10, ...)
lapply(1:20, f)  # the same
lapply(1:20, f, replace=TRUE) # a second argument

See ?lapply

Hope this helps,

Rui Barradas

Em 30-06-2012 20:34, solafah bh escreveu:

Hello
I have a loop to sample 20 samples and I want to put them in one list, how I 
can make this??

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


__
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] loop in list

2012-06-30 Thread R. Michael Weylandt
I might think replicate() is slightly more idiomatic, but I'm not in a position 
to check if simplify=FALSE will keep a list. 

Best,
Michael

On Jun 30, 2012, at 7:13 PM, Rui Barradas  wrote:

> Hello,
> 
> You can avoid the loop using lapply.
> 
> f <- function(x) sample(100, 10)
> samp.list <- lapply(1:20, f)
> 
> will choose 20 samples of 10 integers up to 100 and put them in a list. All 
> you need is to write a function f(). f() must have an argument, even if it 
> doesn't use it. If you need other arguments to be processed by f(), define it 
> and call it as, for instance using the example above,
> 
> f <- function(x, ...) sample(100, 10, ...)
> lapply(1:20, f)  # the same
> lapply(1:20, f, replace=TRUE) # a second argument
> 
> See ?lapply
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 30-06-2012 20:34, solafah bh escreveu:
>> Hello
>> I have a loop to sample 20 samples and I want to put them in one list, how I 
>> can make this??
>> 
>> Regards
>> Sulafah
>>[[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.

__
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] loop in list

2012-06-30 Thread Rui Barradas

Hello,

You can avoid the loop using lapply.

f <- function(x) sample(100, 10)
samp.list <- lapply(1:20, f)

will choose 20 samples of 10 integers up to 100 and put them in a list. 
All you need is to write a function f(). f() must have an argument, even 
if it doesn't use it. If you need other arguments to be processed by 
f(), define it and call it as, for instance using the example above,


f <- function(x, ...) sample(100, 10, ...)
lapply(1:20, f)  # the same
lapply(1:20, f, replace=TRUE) # a second argument

See ?lapply

Hope this helps,

Rui Barradas

Em 30-06-2012 20:34, solafah bh escreveu:

Hello
I have a loop to sample 20 samples and I want to put them in one list, how I 
can make this??

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


Re: [R] loop in list

2012-06-30 Thread arun
Hi,

Try this,

list1<-list()
vec<-rnorm(15,25)

for(i in 1:20)
{
list1[[i]]<-sample(vec,replace=FALSE)
}
list1

[[1]]
 [1] 24.28594 25.05309 25.48962 24.71479 22.48122 25.41300 25.26129 25.15602
 [9] 24.91442 23.65078 26.84776 24.85934 25.00111 24.16320 27.05351

[[2]]
 [1] 24.91442 24.28594 25.05309 24.16320 24.71479 22.48122 25.26129 26.84776
 [9] 25.00111 25.41300 27.05351 25.48962 25.15602 24.85934 23.65078
---


A.K.



- Original Message -
From: solafah bh 
To: R help mailing list 
Cc: 
Sent: Saturday, June 30, 2012 3:34 PM
Subject: [R] loop in list

Hello
I have a loop to sample 20 samples and I want to put them in one list, how I 
can make this?? 
 
Regards
Sulafah
    [[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.


Re: [R] loop in list

2012-06-30 Thread Greg Snow
Instead of a loop you can use the replicate or lapply functions which
will create lists for you.

otherwise you can start with an empty list (mylist <- list() )

then add to the list in each iteration of the loop:

for(i in 1:10) {
mylist[[i]] <- myfunction(i)
}



On Sat, Jun 30, 2012 at 1:34 PM, solafah bh  wrote:
> Hello
> I have a loop to sample 20 samples and I want to put them in one list, how I 
> can make this??
>
> Regards
> Sulafah
> [[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.
>



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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.


[R] loop in list

2012-06-30 Thread solafah bh
Hello
I have a loop to sample 20 samples and I want to put them in one list, how I 
can make this?? 
 
Regards
Sulafah
[[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.