Skip to content

Assigning a vector to every element of a list.

8 messages · Gabor Grothendieck, Patrick Burns, Spencer Maynes +2 more

#
On 2012-07-02 15:16, Spencer Maynes wrote:
lapply( b, function(x) x[] <- d )

Peter Ehlers
#
On Mon, Jul 2, 2012 at 6:16 PM, Spencer Maynes <smaynes89 at gmail.com> wrote:
Try this where the first line creates a list, L, whose elements we
want to replace and the second line replaces every element with the
indicated vector:
[[1]]
[1] 1 2 3 4

[[2]]
[1] 1 2 3 4

[[3]]
[1] 1 2 3 4
#
b <- rep(list(d), length(b))
On 02/07/2012 23:16, Spencer Maynes wrote:

  
    
#
Hi,

Glad all of them worked.? In my reply to you, my first solution was:
list2<-lapply(1:10,function(x) vec1)
The more generic form should be:

list2<-lapply(1:length(list1),function(x) vec1)


A.K.

----- Original Message -----
From: Spencer Maynes <smaynes89 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, July 3, 2012 12:47 PM
Subject: Re: [R] Assigning a vector to every element of a list.

Thanks guys for the help, I'm going to go with Patrick Burns answer because
it seems to work the best for my situation, but these all seem like they
should work.
On Tue, Jul 3, 2012 at 2:51 AM, Patrick Burns <pburns at pburns.seanet.com>wrote:

            
??? [[alternative HTML version deleted]]

______________________________________________
R-help at 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.
#
On 2012-07-03 09:47, Spencer Maynes wrote:
Patrick's solution is similar to Gabor's, but, personally,
I favour Gabor's. Seems neatest and simplest to me.

Peter Ehlers