Skip to content

Question on list objects

4 messages · Ron Michael, Gabor Grothendieck, Charles C. Berry +1 more

#
On Sat, Jan 8, 2011 at 6:26 AM, Ron Michael <ron_michael70 at yahoo.com> wrote:
Try these:

rep(list(dat), 3)

Map(`+`, list1, list2)
#
On Sat, 8 Jan 2011, Ron Michael wrote:

            
See

 	?rep
 	...
 	Examples
 	...
 	## replicate a list
 	...
See

 	?mapply

 	take note of the SIMPLIFY arg, which you want as FALSE

HTH,

Chuck
Charles C. Berry                            Dept of Family/Preventive Medicine
cberry at tajo.ucsd.edu			    UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
#
Hi Ron,

Here you go.

Cheers,

Josh

dat <- matrix(1:9,3)
ldat <- list(dat, dat, dat)
ldat[[1]] == dat

## Or
lapply(1:3, function(x) dat)
## which is similar in spirit to
output <- vector("list", 3)
for(i in 1:3) {
  output[[i]] <- dat
}

list1 <- list2 <- vector("list", length=2)

for(i in 1:2) {
  list1[[i]] <- matrix(rnorm(15), 3)
  list2[[i]] <- matrix(rnorm(15), 3)
 }

lapply(1:2, function(x) {list1[[x]] + list2[[x]]})

## This can be done more simply
lapply(1:2, function(x) {matrix(rnorm(15), 3) + matrix(rnorm(15), 3)})
On Sat, Jan 8, 2011 at 3:26 AM, Ron Michael <ron_michael70 at yahoo.com> wrote: