Sunny Srivastava
2010-Sep-13 18:41 UTC
[R] Question: Form a new list with the index replicated equal to the number of elements in that index
Dear R-Helpers, I have a list l1 like: l1[[1]] a b c l1[[2]] d l1[[3]] e f I want an output res like: res[[1]] 1 1 1 res[[2]] 2 res[[3]] 3 3 Essentially, I want to replicate each index equal to the number of elements present in that index. Below is what I do to accomplish this: l1 <- list(c("a", "b", "c"), "d", c("e", "f")) res <- mapply(rep, seq_along(l1),times=(lapply(l1, length))) Is there a more elegant way of doing this (possibly using one (l/m)apply function)? Thanks in advance for the help. Best Regards, S. [[alternative HTML version deleted]]
Peter Dalgaard
2010-Sep-13 20:20 UTC
[R] Question: Form a new list with the index replicated equal to the number of elements in that index
On 09/13/2010 08:41 PM, Sunny Srivastava wrote:> Dear R-Helpers, > I have a list l1 like: > > l1[[1]] > a b c > > l1[[2]] > d > > l1[[3]] > e f > > I want an output res like: > > res[[1]] > 1 1 1 > > res[[2]] > 2 > > res[[3]] > 3 3 > > Essentially, I want to replicate each index equal to the number of elements > present in that index. > > Below is what I do to accomplish this: > > l1 <- list(c("a", "b", "c"), "d", c("e", "f")) > res <- mapply(rep, seq_along(l1),times=(lapply(l1, length))) > > Is there a more elegant way of doing this (possibly using one (l/m)apply > function)?Don't know about elegance, but how about lapply(seq_along(l1), function(i)rep(i, length(l1[[j]])) ) -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Henrique Dallazuanna
2010-Sep-13 20:24 UTC
[R] Question: Form a new list with the index replicated equal to the number of elements in that index
Try this: relist(rep(1:length(l1), sapply(l1, length)), l1) On Mon, Sep 13, 2010 at 3:41 PM, Sunny Srivastava <research.baba@gmail.com>wrote:> Dear R-Helpers, > I have a list l1 like: > > l1[[1]] > a b c > > l1[[2]] > d > > l1[[3]] > e f > > I want an output res like: > > res[[1]] > 1 1 1 > > res[[2]] > 2 > > res[[3]] > 3 3 > > Essentially, I want to replicate each index equal to the number of elements > present in that index. > > Below is what I do to accomplish this: > > l1 <- list(c("a", "b", "c"), "d", c("e", "f")) > res <- mapply(rep, seq_along(l1),times=(lapply(l1, length))) > > Is there a more elegant way of doing this (possibly using one (l/m)apply > function)? > > Thanks in advance for the help. > > Best Regards, > S. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Phil Spector
2010-Sep-13 21:04 UTC
[R] Question: Form a new list with the index replicated equal to the number of elements in that index
Sunny - I don't think mapply is needed:> lapply(1:length(mylist),function(x)rep(x,length(mylist[[x]])))[[1]] [1] 1 1 1 [[2]] [1] 2 [[3]] [1] 3 3 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Mon, 13 Sep 2010, Sunny Srivastava wrote:> Dear R-Helpers, > I have a list l1 like: > > l1[[1]] > a b c > > l1[[2]] > d > > l1[[3]] > e f > > I want an output res like: > > res[[1]] > 1 1 1 > > res[[2]] > 2 > > res[[3]] > 3 3 > > Essentially, I want to replicate each index equal to the number of elements > present in that index. > > Below is what I do to accomplish this: > > l1 <- list(c("a", "b", "c"), "d", c("e", "f")) > res <- mapply(rep, seq_along(l1),times=(lapply(l1, length))) > > Is there a more elegant way of doing this (possibly using one (l/m)apply > function)? > > Thanks in advance for the help. > > Best Regards, > S. > > [[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. >
Reasonably Related Threads
- [plyr] Question regarding ddply: use of .(as.name(varname)) and varname in ddply function
- Question: how to obtain the clusters of genes (basically the ones in the row dendrograms) from an object obtained by heatmap.2 function
- Using apply function on duplicates in a data.frame
- Wilcoxon Rank-Sum Test with Bonferroni's correction
- relist() is broken when the skeleton is a list with empty list elements