I'm having troubles with the names of columns. quantmod deal with stock quotes. I've created an array of the first 5 closing prices from Jan 2007. (Is there a problem that the name is the same as the variable name? There shouldn't be.)> closeclose 2007-01-03 1416.60 2007-01-04 1418.34 2007-01-05 1409.71 2007-01-08 1412.84 2007-01-09 1412.11 When I try to create a more complex array by adding columns, the names get fouled up. Here's a simple example.> cbind(changed.close = close+1, zero = 0, close)close zero close.1 2007-01-03 1417.60 0 1416.60 2007-01-04 1419.34 0 1418.34 2007-01-05 1410.71 0 1409.71 2007-01-08 1413.84 0 1412.84 2007-01-09 1413.11 0 1412.11 The first column should be called "changed.close", but it's called "close". The second column has the right name. The third column should be called "close" but it's called "close.1". Why is that? Am I missing something? If I change the order of the columns and let close have its original name, there is still a problem.> cbind(close, zero = 0, changed.close = close+1)close zero close.1 2007-01-03 1416.60 0 1417.60 2007-01-04 1418.34 0 1419.34 2007-01-05 1409.71 0 1410.71 2007-01-08 1412.84 0 1413.84 2007-01-09 1412.11 0 1413.11 Now the names on the first two columns are ok, but the third column is still wrong. Again, why is that? Apparently it's not letting me assign a name to a column that comes from something that already has a name. Is that the way it should be? I don't get that same problem on a simpler example.> IX <- cbind(I=0, X=(1:3))> IX I X [1,] 0 1 [2,] 0 2 [3,] 0 3> cbind(Y = 1, Z = IX[, "I"], W = IX[, "X"])Y Z W [1,] 1 0 1 [2,] 1 0 2 [3,] 1 0 3 Is this a peculiarity to xts objects? Thanks. *-- Russ * * * P.S. Once again I feel frustrated because it's taken me far more time than it deserves to track down and characterize this problem. I can fix it by using the names function. But I shouldn't have to do that. [[alternative HTML version deleted]]
On May 8, 2011, at 3:07 PM, Russ Abbott wrote:> I'm having troubles with the names of columns. > > quantmod deal with stock quotes. I've created an array of the first 5 > closing prices from Jan 2007. (Is there a problem that the name is > the same > as the variable name? There shouldn't be.) > >> close > > close > > 2007-01-03 1416.60 > > 2007-01-04 1418.34 > > 2007-01-05 1409.71 > > 2007-01-08 1412.84 > > 2007-01-09 1412.11 > > > When I try to create a more complex array by adding columns, the > names get > fouled up. Here's a simple example. > >> cbind(changed.close = close+1, zero = 0, close)I suspect that you are actually using xts objects that you are incorrectly calling 'array's. If something is puzzling about the behavior of an R object the first thing to do is see what you are really dealing with so ... str(object) If you load the xts package and type ?cbind.xts , you get a help page for merge.xts. (In base R I do not know of a way to assign columns the way you propose within a `merge` call.) Here is the code for cbind.xts: > cbind.xts function (..., all = TRUE, fill = NA, suffixes = NULL) { merge.xts(..., all = all, fill = fill, suffixes = suffixes) } <environment: namespace:xts>> > close zero close.1 > > 2007-01-03 1417.60 0 1416.60 > > 2007-01-04 1419.34 0 1418.34 > > 2007-01-05 1410.71 0 1409.71 > > 2007-01-08 1413.84 0 1412.84 > > 2007-01-09 1413.11 0 1412.11 > > > The first column should be called "changed.close", but it's called > "close". > The second column has the right name. The third column should be > called > "close" but it's called "close.1". Why is that? Am I missing > something? > > If I change the order of the columns and let close have its original > name, > there is still a problem. > >> cbind(close, zero = 0, changed.close = close+1) > > close zero close.1 > > 2007-01-03 1416.60 0 1417.60 > > 2007-01-04 1418.34 0 1419.34 > > 2007-01-05 1409.71 0 1410.71 > > 2007-01-08 1412.84 0 1413.84 > > 2007-01-09 1412.11 0 1413.11 > > > Now the names on the first two columns are ok, but the third column > is still > wrong. Again, why is that? Apparently it's not letting me assign a > name to > a column that comes from something that already has a name. Is that > the way > it should be? > > I don't get that same problem on a simpler example. > >> IX <- cbind(I=0, X=(1:3)) > >> IX > > I X > > [1,] 0 1 > > [2,] 0 2 > > [3,] 0 3 > >> cbind(Y = 1, Z = IX[, "I"], W = IX[, "X"]) > > Y Z W > > [1,] 1 0 1 > > [2,] 1 0 2 > > [3,] 1 0 3 > > > Is this a peculiarity to xts objects? > > Thanks. > > *-- Russ * > * > * > P.S. Once again I feel frustrated because it's taken me far more > time than > it deserves to track down and characterize this problem. I can fix > it by > using the names function. But I shouldn't have to do that. > > [[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.David Winsemius, MD West Hartford, CT
Hi Russ, On Sun, May 8, 2011 at 12:07 PM, Russ Abbott <russ.abbott at gmail.com> wrote:> I'm having troubles with the names of columns. > > quantmod deal with stock quotes. ?I've created an array of the first 5 > closing prices from Jan 2007. (Is there a problem that the name is the same > as the variable name? There shouldn't be.) > >> close > > ? ? ? ? ? ? close > > 2007-01-03 1416.60 > > 2007-01-04 1418.34 > > 2007-01-05 1409.71 > > 2007-01-08 1412.84 > > 2007-01-09 1412.11It would be appreciated in the future if you provided the object via dput() or some such that is easy to paste in.> > > When I try to create a more complex array by adding columns, the names get > fouled up. ?Here's a simple example. > >> cbind(changed.close = close+1, zero = 0, close) > > ? ? ? ? ? ? close zero close.1 > > 2007-01-03 1417.60 ? ?0 1416.60 > > 2007-01-04 1419.34 ? ?0 1418.34 > > 2007-01-05 1410.71 ? ?0 1409.71 > > 2007-01-08 1413.84 ? ?0 1412.84 > > 2007-01-09 1413.11 ? ?0 1412.11 > > > The first column should be called "changed.close", but it's called "close". > The second column has the right name. The third column should be called > "close" but it's called "close.1". Why is that? Am I missing something?Yes. mat <- matrix(1:10, dimnames = list(NULL, "A")) cbind(X = 11:20, Y = mat + 1) cbind(X = 11:20, Y = mat[, "A"] + 1) In particular note that: class(mat) class(mat[, "A"]) It is silly to expect to be able to pass a single name for an entire matrix, while it makes complete sense that that would work for a vector. The problem with naming the column the same thing as the object containing it, is our limited human minds get ramfeezled (R does just fine, as you can see).> > If I change the order of the columns and let close have its original name, > there is still a problem. > >> cbind(close, zero = 0, changed.close = close+1) > > ? ? ? ? ? ? close zero close.1 > > 2007-01-03 1416.60 ? ?0 1417.60 > > 2007-01-04 1418.34 ? ?0 1419.34 > > 2007-01-05 1409.71 ? ?0 1410.71 > > 2007-01-08 1412.84 ? ?0 1413.84 > > 2007-01-09 1412.11 ? ?0 1413.11 > > > Now the names on the first two columns are ok, but the third column is still > wrong. Again, why is that? ?Apparently it's not letting me assign a name to > a column that comes from something that already has a name. ?Is that the way > it should be? > > I don't get that same problem on a simpler example. > >> IX <- cbind(I=0, X=(1:3)) > > ?> IX > > ? ? I X > > [1,] 0 1 > > [2,] 0 2 > > [3,] 0 3 > >> cbind(Y = 1, Z = IX[, "I"], W = IX[, "X"]) > > ? ? Y Z W > > [1,] 1 0 1 > > [2,] 1 0 2 > > [3,] 1 0 3 > > > Is this a peculiarity to xts objects?Nope, but do check the type of object you are working with---there are often different methods for different objects so I would not assumet that all things would work the same. Cheers, Josh> > Thanks. > > *-- Russ * > * > * > P.S. Once again I feel frustrated because it's taken me far more time than > it deserves to track down and characterize this problem. I can fix it by > using the names function. But I shouldn't have to do that. > > ? ? ? ?[[alternative HTML version deleted]]Please post using plain text.> > ______________________________________________ > 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.-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi Russ, Colnames don't get rewritten if they already exist. The reason is due to performance and how cbind is written at the R level. It isn't perfect per se, but the complexity and variety of dispatch that can take place for cbind in R, as it isn't a generic, is quite challenging to get to behave as one may hope. After years of trying I'd say it is nearly impossible to do what you want without causing horrible memory issues on non trivial objects they are use in production systems **using** xts on objects with billions of rows. Your simple case that has a simple workaround would cost everyone using in the other 99.999% of cases to pay a recurring cost that isn't tolerable. If this is frustrating to you you should stop using the class. Jeff Jeffrey Ryan | Founder | jeffrey.ryan@lemnica.com www.lemnica.com On May 8, 2011, at 2:07 PM, Russ Abbott <russ.abbott@gmail.com> wrote:> I'm having troubles with the names of columns. > > quantmod deal with stock quotes. I've created an array of the first 5 closing prices from Jan 2007. (Is there a problem that the name is the same as the variable name? There shouldn't be.) > > > > close > > > > close > > 2007-01-03 1416.60 > > 2007-01-04 1418.34 > > 2007-01-05 1409.71 > > 2007-01-08 1412.84 > > 2007-01-09 1412.11 > > When I try to create a more complex array by adding columns, the names get fouled up. Here's a simple example. > > > > > cbind(changed.close = close+1, zero = 0, close) > > > > > close zero close.1 > > > 2007-01-03 1417.60 0 1416.60 > > > 2007-01-04 1419.34 0 1418.34 > > > 2007-01-05 1410.71 0 1409.71 > > > 2007-01-08 1413.84 0 1412.84 > > > 2007-01-09 1413.11 0 1412.11 > > The first column should be called "changed.close", but it's called "close". The second column has the right name. The third column should be called "close" but it's called "close.1". Why is that? Am I missing something? > > If I change the order of the columns and let close have its original name, there is still a problem. > > > > cbind(close, zero = 0, changed.close = close+1) > > > > close zero close.1 > > 2007-01-03 1416.60 0 1417.60 > > 2007-01-04 1418.34 0 1419.34 > > 2007-01-05 1409.71 0 1410.71 > > 2007-01-08 1412.84 0 1413.84 > > 2007-01-09 1412.11 0 1413.11 > > Now the names on the first two columns are ok, but the third column is still wrong. Again, why is that? Apparently it's not letting me assign a name to a column that comes from something that already has a name. Is that the way it should be? > > I don't get that same problem on a simpler example. > > > > > IX <- cbind(I=0, X=(1:3)) > > IX > > I X > > [1,] 0 1 > > [2,] 0 2 > > [3,] 0 3 > > > cbind(Y = 1, Z = IX[, "I"], W = IX[, "X"]) > > Y Z W > > [1,] 1 0 1 > > [2,] 1 0 2 > > [3,] 1 0 3 > > Is this a peculiarity to xts objects? > > Thanks. > > -- Russ > > P.S. Once again I feel frustrated because it's taken me far more time than it deserves to track down and characterize this problem. I can fix it by using the names function. But I shouldn't have to do that.[[alternative HTML version deleted]]