[R] Repeating values in a list

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sun Sep 19 07:12:11 CEST 2010


> have <- list(a=7,b=3,c=1)
> have2 <- lapply(have, rep, 2)
> have2
$a
[1] 7 7

$b
[1] 3 3

$c
[1] 1 1

>  

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Derek Ogle
Sent: Sunday, 19 September 2010 11:20 AM
To: R (r-help at R-project.org)
Subject: [R] Repeating values in a list

I have a list that looks like this ...

> have <- list(a=7,b=3,c=1)
> have
$a
[1] 7

$b
[1] 3

$c
[1] 1

and I want to have a simple way to change it to the following without re-typing the values ...

> desire <- list(a=c(7,7),b=c(3,3),c=c(1,1))
> desire
$a
[1] 7 7

$b
[1] 3 3

$c
[1] 1 1

In other words, I need to create the list in desire from the list in have.

In my "real" work the number of items in the list may be more than three and the number of times to repeat the numbers may be greater than two.

Thank you in advance for any help you can offer.

______________________________________________
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