[R] Assigning a vector to every element of a list.

arun smartpink111 at yahoo.com
Tue Jul 3 19:10:03 CEST 2012


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:

> b <- rep(list(d), length(b))
>
>
> On 02/07/2012 23:16, Spencer Maynes wrote:
>
>> I have a vector d of unknown length, and a list b of unknown length. I
>> would like to replace every element of b with d. Simply writing b<-d does
>> not work as R tries to fit every element of d to a different element of d,
>> and b<-rep(d,length(b)) does not work either as it makes a list of
>> length length(d)*length(b) not a list of length(b). I know how to do this
>> with a for loop, but I feel that there has to be a more efficient way. Any
>> suggestions?
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________**________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
> --
> Patrick Burns
> pburns at pburns.seanet.com
> twitter: @portfolioprobe
> http://www.portfolioprobe.com/**blog <http://www.portfolioprobe.com/blog>
> http://www.burns-stat.com
> (home of 'Some hints for the R beginner'
> and 'The R Inferno')
>
>
>

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




More information about the R-help mailing list