s-news
[Top] [All Lists]

Summary on Tricky Merging of a vector and a list

To: s-news@lists.biostat.wustl.edu
Subject: Summary on Tricky Merging of a vector and a list
From: Sven.Knudsen@adeptscience.dk
Date: Thu, 20 Mar 2003 16:03:35 +0100
Thank you all very much (None mentioned, none forgotten).

There were many suggestions (10), and one I really like

a <- split(rnorm(1000*10), rep(1:1000, each=10)) # your list
b <- 1:1000 # your b vector
########
a.n <- lapply(1:length(a), function(i, a, b) c(b[i], a[[i]]), a, b)

Others have suggested using cbind of matrice or data.frame objects. These
methods require use of unlist or as.data.frame, and one drawback is such a
conversion does take time compared to the above solution.

Again I would like to thank all contributors.

Kind regards
Sven
Adept Scientific Aps



|---------+------------------------------------>
|         |           Sven.Knudsen@adeptscience|
|         |           .dk                      |
|         |           Sent by:                 |
|         |           s-news-owner@lists.biosta|
|         |           t.wustl.edu              |
|         |                                    |
|         |                                    |
|         |           03/20/2003 11:02 AM      |
|         |                                    |
|---------+------------------------------------>
  
>------------------------------------------------------------------------------------------------------------------------------|
  |                                                                             
                                                 |
  |       To:       s-news@lists.biostat.wustl.edu                              
                                                 |
  |       cc:                                                                   
                                                 |
  |       Subject:  [S] Tricky Merging of a vector and a list                   
                                                 |
  
>------------------------------------------------------------------------------------------------------------------------------|





Hi S-friends

I have a small, but to me tricky problem.

Let a denote a list, say
> a <- list()
> a$n1 <- 1:2
> a$n2 <- 3:4
> a
$n1:
[1] 1 2

$n2:
[1] 3 4

Let b denote a vector of same length as a, and assume that

>b <- c(0,12)

I know want to create the following merged list of a and b, consisting of a
   [[i]] <- c(b[i],a[[i]])
> a
$n1:
[1] 0 1 2

$n2:
[1] 12  3  4

No problems in using a loop - but my list's are very long.

Any suggestions to avoid a loop will be appreciated

Kind regards

Sven
Adept Scientific Aps


--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news






<Prev in Thread] Current Thread [Next in Thread>
  • Summary on Tricky Merging of a vector and a list, Sven . Knudsen <=