It works perfectly! Thank you, Gabor, for the quick solution and for letting me learn sub() function. Supreme! :-)
Regards, Sergey On Tue, Feb 3, 2009 at 15:05, Gabor Grothendieck <ggrothendi...@gmail.com> wrote: > Not sure why you need to have all these extra columns if they are only > zero anyways. Could you not append them when you get data for them? > Are these placeholders really of any value? At any rate its done using > cbind or merge like this: > > library(zoo) > library(chron) > > nms <- sub(".Comdty", "tr", me.la.tickers) > zeromat <- matrix(0, nrow(me.la), length(nms), dimnames = list(NULL, nms)) > cbind(me.la, zeromat) > > In this case zeromat and me.la have the same dimensions so we could > alternately reduce it to this slightly briefer code: > > zeromat <- 0 * me.la > colnames(zeromat) <- sub(".Comdty", "tr", me.la.tickers) > cbind(me.la, zeromat) > > > On Tue, Feb 3, 2009 at 8:44 AM, Sergey Goriatchev <serg...@gmail.com> wrote: >> Hello, everyone >> >> I have a question. >> >> Assume I have the following zoo object: >> >> me.la <- structure(c(1524.75, 1554.5, 1532.25, 1587.5, 1575.25, 1535.5, >> 1550, 1493.5, 1492.5, 1472.25, 1457.5, 1442.75, 1399, 1535.75, >> 1565.25, 1543.5, 1598.5, 1586.5, 1547, 1561.5, 1504.75, 1503.75, >> 1483.75, 1468.75, 1453.75, 1410, 1546.75, 1575.25, 1554, 1609, >> 1597.5, 1558.5, 1573, 1516.25, 1515.5, 1495, 1480, 1465, 1421.25, >> 1561.5, 1590, 1568.75, 1623.5, 1612, 1573, 1587.5, 1530.5, 1530, >> 1509.75, 1494.5, 1479.5, 1435.75, 1573.5, 1601.5, 1580.25, 1635, >> 1623.5, 1584.5, 1599, 1541.75, 1541.5, 1521.5, 1506, 1491, 1447.25, >> 1585.5, 1613, 1591.75, 1646, 1634.5, 1595.5, 1610, 1552.75, 1552.75, >> 1532.75, 1517, 1502, 1458.25, 1600, 1627.5, 1606.5, 1660, 1649, >> 1609.75, 1624.25, 1567, 1567, 1547, 1531, 1516, 1472.25, 1612, >> 1639.5, 1618.25, 1671.5, 1661, 1621.5, 1635.75, 1578, 1578, 1558, >> 1542, 1527, 1483.5), .Dim = c(13L, 8L), index = structure(c(14245, >> 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258, >> 14259, 14260, 14263), format = "m/d/y", origin = structure(c(1, >> 1, 1970), .Names = c("month", "day", "year")), class = c("dates", >> "times")), class = "zoo", .Dimnames = list(NULL, c("LA1 COMDTY", >> "LA2 COMDTY", "LA3 COMDTY", "LA4 COMDTY", "LA5 COMDTY", "LA6 COMDTY", >> "LA7 COMDTY", "LA8 COMDTY"))) >> >> I also have a following variable (used in RBloomberg) in my environment: >> >> me.la.tickers <- c("LA1 Comdty", "LA2 Comdty", "LA3 Comdty", "LA4 >> Comdty", "LA5 Comdty", "LA6 Comdty", "LA7 Comdty", "LA8 Comdty") >> >> What I need to do is to automate the following manual piece of code: >> >> me.la$LA2tr <- 0 >> me.la$LA3tr <- 0 >> me.la$LA4tr <- 0 >> me.la$LA5tr <- 0 >> me.la$LA6tr <- 0 >> me.la$LA7tr <- 0 >> me.la$LA8tr <- 0 >> >> Basically, I need to automatically create new columns in futures >> object taking first part of names in me.la.tickers. >> >> I tried with paste() and assign() combination, but could not get. >> >> I could do the manual part, of course, it does not take much time, but: >> >> 1) I want to learn how to automatically create new columns in zoo objects >> 2) I have many more variables that have to be treated similarly (that >> is, I have me.lp with corresponding me.lp.tickers, me.qc with >> me.qc.tickers, etc. Then I have a bunch of variables starting with >> "en", like en.co and en.cl, and corresponding ticker vectors, then >> "ag" variables and "so" variables, as well). I have all in all 24 zoo >> variables with 24 corresponding ticker vectors, and for each a >> corresponding ticker vector, and for each zoo variable I need to >> create 7 extra columns. That would take much time to do manually, and >> a lot of code. >> >> How would I do this automatically, please? >> >> Thank you in advance for your help! >> >> Regards, >> Sergey >> >> ______________________________________________ >> 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. >> > -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten ______________________________________________ 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.