Hi How do I concatenate two lists element-by-element? Example: list.1 <- list(temperature=c("hot","cold") , size=c("big","medium")) list.2 <- list(temperature=c("lukewarm") , size=c("massive","tiny")) list.wanted <- list(temperature=c("hot","cold","lukewarm") , size=c("big","medium","massive","tiny")) (the lists are dimnames() of an array). -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)
On Mon, 18 Oct 2004, Robin Hankin wrote:> How do I concatenate two lists element-by-element?> Example: > > list.1 <- list(temperature=c("hot","cold") , size=c("big","medium")) > list.2 <- list(temperature=c("lukewarm") , size=c("massive","tiny")) > > > list.wanted <- list(temperature=c("hot","cold","lukewarm") , > size=c("big","medium","massive","tiny")) > > > > (the lists are dimnames() of an array).mapply(c, list.1, list.2) (but it loses the list names, so copy them across). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I don't know of a way other than brute force:> list.3 <- list() > for (i in union(names(list.1),names(list.2)))list.3[[i]] <- union(list.1[[i]],list.2[[i]])> list.3$temperature [1] "hot" "cold" "lukewarm" $size [1] "big" "medium" "massive" "tiny" I suppose one could write a method for "merge" for lists. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Robin Hankin Sent: Monday, October 18, 2004 8:01 AM To: r-help at stat.math.ethz.ch Subject: [R] concatenating lists elementwise Hi How do I concatenate two lists element-by-element? Example: list.1 <- list(temperature=c("hot","cold") , size=c("big","medium")) list.2 <- list(temperature=c("lukewarm") , size=c("massive","tiny")) list.wanted <- list(temperature=c("hot","cold","lukewarm") , size=c("big","medium","massive","tiny")) (the lists are dimnames() of an array). -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution) ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html