Assigning a vector to every element of a list.

9 messages
Open this post in threaded view
|
Report Content as Inappropriate

Assigning a vector to every element of a list.

 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Assigning a vector to every element of a list.

 On 2012-07-02 15: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? lapply( b, function(x) x[] <- d ) Peter Ehlers ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Assigning a vector to every element of a list.

 In reply to this post by Spencer On Mon, Jul 2, 2012 at 6:16 PM, Spencer Maynes <[hidden email]> 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? > 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: > L <- list(1, 1:2, "abc") > L[] <- list(1:4) > L [[1]] [1] 1 2 3 4 [[2]] [1] 1 2 3 4 [[3]] [1] 1 2 3 4 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Assigning a vector to every element of a list.

 In reply to this post by Spencer Hi, If you want to assign a vector to every element of a list, vec1<-11:20 list1<-split(LETTERS[1:10],1:length(LETTERS[1:10])) list2<-lapply(1:10,function(x) vec1) or, list3<-lapply(list1,function(x) list1=vec1) or list4<-list() vec2<-1:5 list4[1:length(list1)]<-list(vec2) # if you want to assign each element of vector to each element of list (assuming both have the same lengths), vec1<-11:20 list1<-split(LETTERS[1:10],1:length(LETTERS[1:10])) newlist<-split(vec1,1:length(vec1)) A.K. ----- Original Message ----- From: Spencer Maynes <[hidden email]> To: [hidden email] Cc: Sent: Monday, July 2, 2012 6:16 PM Subject: [R] Assigning a vector to every element of a list. 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Assigning a vector to every element of a list.

 In reply to this post by Spencer 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]] > > ______________________________________________ > [hidden email] 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. > -- Patrick Burns [hidden email] twitter: @portfolioprobe http://www.portfolioprobe.com/bloghttp://www.burns-stat.com(home of 'Some hints for the R beginner' and 'The R Inferno') ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: 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 <[hidden email]>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]] >> >> ______________________________**________________ >> [hidden email] 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. >> >> > -- > Patrick Burns > [hidden email] > twitter: @portfolioprobe > 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Assigning a vector to every element of a list.

 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 <[hidden email]> To: [hidden email] 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 <[hidden email]>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]] >> >> ______________________________**________________ >> [hidden email] 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. >> >> > -- > Patrick Burns > [hidden email] > twitter: @portfolioprobe > 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Assigning a vector to every element of a list.

 In reply to this post by Spencer On 2012-07-03 09:47, Spencer Maynes wrote: > 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. Patrick's solution is similar to Gabor's, but, personally, I favour Gabor's. Seems neatest and simplest to me. Peter Ehlers > > On Tue, Jul 3, 2012 at 2:51 AM, Patrick Burns <[hidden email]>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]] >>> >>> ______________________________**________________ >>> [hidden email] 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. >>> >>> >> -- >> Patrick Burns >> [hidden email] >> twitter: @portfolioprobe >> 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]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.