[R] Repeating values in a list
Derek Ogle
DOgle at northland.edu
Sun Sep 19 05:18:59 CEST 2010
Thanks to Dennis for solving my first question and also pointing me in the right direction.
To complete the thread ... there are times when I need to have different numbers of repeats for each object in the list. This can be accomplished with mapply() as follows ...
> mapply(rep,x=have,c(1,1,2))
$a
[1] 7
$b
[1] 3
$c
[1] 1 1
From: Dennis Murphy [mailto:djmuser at gmail.com]
Sent: Saturday, September 18, 2010 8:25 PM
To: Derek Ogle
Cc: R (r-help at R-project.org)
Subject: Re: [R] Repeating values in a list
Hi:
How about
> have <- list(a=7,b=3,c=1)
> lapply(have, rep, 2)
$a
[1] 7 7
$b
[1] 3 3
$c
[1] 1 1
HTH,
Dennis
On Sat, Sep 18, 2010 at 6:19 PM, Derek Ogle <DOgle at northland.edu> wrote:
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