Try this: mapply(function(x, y)assign(x, y, envir = globalenv()), sprintf('vec.%s', letters[1:length(vec)]), split(diag(vec), 1:length(vec)))
On Tue, Aug 17, 2010 at 7:57 AM, Karen Kotschy <ka...@sevenc.co.za> wrote: > Dear helpRs > > Does anyone have an elegant way of doing the following: > > For a given numeric vector, e.g. vec <- c(3,2,6,4,7) > > Create a series of vectors where all but 1 of the values are replaced by > 0's, e.g. > > vec.a <- c(3,0,0,0,0) > vec.b <- c(0,2,0,0,0) > vec.c <- c(0,0,6,0,0) > vec.d <- c(0,0,0,4,0) > vec.e <- c(0,0,0,0,7) > > I have looked at `replace', but can't think of a way of making it produce > the 5 lines above without a for loop. > > I would also like to assign the names automatically. I can create them > easily using paste, but how does one get R to treat the resulting > character strings as object names to which values can be assigned? > > Thanks! > Karen > > --- > Karen Kotschy > Centre for Water in the Environment > University of the Witwatersrand, Johannesburg, South Africa > > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean. > > ______________________________________________ > 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]]
______________________________________________ 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.