I was wondering why the following doesn't work: > a=c(1,2) > names(a)=c("one","two") > a one two 1 2 > > names(a[2]) [1] "two" > > names(a[2])="too" > names(a) [1] "one" "two" > a one two 1 2 I must not be understanding some basic concept here. Why doesn't the 2nd name change to "too"? also unrelated: if I have two vectors and I want to combine them to form a matrix ,is cbind (or rbind) the most direct way to do this? e.g. x=c(1,2,3) y=c(3,4,5) z=rbind(x,y) alternatively: is there a way to make a matrix with dim=2,3 and then to replace the 2nd row with y something like this (which doesn't work but perhaps there is another way to do the equivalent?) attr(x,"dim")=c(2,3) x[2,]=y
On Feb 24, 2009, at 11:36 PM, Fuchs Ira wrote:> I was wondering why the following doesn't work: > > > a=c(1,2) > > names(a)=c("one","two") > > a > one two > 1 2 > > > > names(a[2]) > [1] "two" > > > > names(a[2])="too" > > names(a) > [1] "one" "two" > > a > one two > 1 2 > > I must not be understanding some basic concept here. > Why doesn't the 2nd name change to "too"?I cannot tell you why, perhaps you are not actually working with the names of a, but I can show you that: > names(a)[2] <- "too" > a[2] too 2 > a one too 1 2 And this is seen as well in the help page examples. The help page also says the following, which I cannot understand: It is possible to update just part of the names attribute via the general rules: see the examples. This works because the expression there is evaluated as z <- "names<-"(z, "[<-"(names(z), 3, "c2")).> > also unrelated: if I have two vectors and I want to combine them to > form a matrix ,is cbind (or rbind) the most direct way to do this? > > e.g. > > x=c(1,2,3) > y=c(3,4,5) > z=rbind(x,y)That is ok. Also effective would be: z <- matrix( c(x,y), ncol=length(x) )> > > alternatively: is there a way to make a matrix with dim=2,3 and then > to replace the 2nd row with y > > something like this (which doesn't work but perhaps there is another > way to do the equivalent?) > > attr(x,"dim")=c(2,3) > x[2,]=yNot sure why you are trying to do that to x since it is a vector but it can be done if you make it a matrix first. Take a look at these and see if you can figure out what is happening: > x <- matrix(x,2,3) # second and third arguments of matrix function are nrow and ncol. > x [,1] [,2] [,3] [1,] 1 3 2 [2,] 2 1 3 And then try: > x = c(1,2,3) > x <- matrix(x,2,3,byrow=TRUE) > x [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 2 3 And now that x is a matrix, this will work: > x[2,] <- y > x [,1] [,2] [,3] [1,] 1 2 3 [2,] 3 4 5 -- David Winsemius> > > ______________________________________________ > 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.
On Feb 25, 2009, at 12:12 AM, David Winsemius wrote:> > On Feb 24, 2009, at 11:36 PM, Fuchs Ira wrote: > >> > > >> >> also unrelated: if I have two vectors and I want to combine them >> to form a matrix ,is cbind (or rbind) the most direct way to do this? >> >> e.g. >> >> x=c(1,2,3) >> y=c(3,4,5) >> z=rbind(x,y) > > That is ok. Also effective would be: > > z <- matrix( c(x,y), ncol=length(x) ) >>Would need byrow=TRUE for that to behave as desired. > z <- matrix(c(x,y), 2.3, byrow=TRUE) > z [,1] [,2] [,3] [1,] 1 2 3 [2,] 3 4 5 -- David Winsemius
Hi Ira: For your first question, under the hood of R, names<- is actually a function so , when you do that, you need to say names(a)[2] rather than names(a[2]). why this is is tricky and I wouldn't do it justice if i tried to explain it. it's best if you do ?"names<-" at an R prompt and read that. For the second one, you can rbind x with anything that of length one and the recycling concept in R will add the extra now but maybe there's a better way that someone else will hopefully send. #================================================================================= a=c(1,2) names(a)=c("one","two") names(a[2]) names(a)[2]<-"too" names(a) #================================================================================== x=c(1,2,3) y=c(3,4,5) x <- matrix(x,nrow=1) print(x) x <- rbind(x,NA) x[2,] <- y print(x) On Tue, Feb 24, 2009 at 11:36 PM, Fuchs Ira wrote:> I was wondering why the following doesn't work: > >> a=c(1,2) >> names(a)=c("one","two") >> a > one two > 1 2 >> >> names(a[2]) > [1] "two" >> >> names(a[2])="too" >> names(a) > [1] "one" "two" >> a > one two > 1 2 > > I must not be understanding some basic concept here. > Why doesn't the 2nd name change to "too"? > > also unrelated: if I have two vectors and I want to combine them to > form a matrix ,is cbind (or rbind) the most direct way to do this? > > e.g. > > x=c(1,2,3) > y=c(3,4,5) > z=rbind(x,y) > > alternatively: is there a way to make a matrix with dim=2,3 and then > to replace the 2nd row with y > > something like this (which doesn't work but perhaps there is another > way to do the equivalent?) > > attr(x,"dim")=c(2,3) > x[2,]=y > > ______________________________________________ > 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.
Fuchs Ira wrote:> I was wondering why the following doesn't work: > > > a=c(1,2) > > names(a)=c("one","two") > > a > one two > 1 2 > > > > names(a[2]) > [1] "two" > > > > names(a[2])="too" > > names(a) > [1] "one" "two" > > a > one two > 1 2 > > I must not be understanding some basic concept here. > Why doesn't the 2nd name change to "too"?because a[2] becomes a newly allocated vector once you make the assignment, and so the assignment does not affect a. however: names(a)[2] = 'too' will affect a the way you seem to wish.> > also unrelated: if I have two vectors and I want to combine them to > form a matrix ,is cbind (or rbind) the most direct way to do this? > > e.g. > > x=c(1,2,3) > y=c(3,4,5) > z=rbind(x,y) > > alternatively: is there a way to make a matrix with dim=2,3 and then > to replace the 2nd row with y > > something like this (which doesn't work but perhaps there is another > way to do the equivalent?) > > attr(x,"dim")=c(2,3) > x[2,]=yyou can do this: z = matrix(c(x, y), nrow=2, ncol=3, byrow=TRUE) but rbind seems much simpler. vQ
David, Wacek: Just so everyone knows, I just looked and this is explained quite clearly in the R Language Reference manual, very similarly to what Wacek did below. On Wed, Feb 25, 2009 at 3:42 AM, Wacek Kusnierczyk wrote:> David Winsemius wrote: >> >> On Feb 24, 2009, at 11:36 PM, Fuchs Ira wrote: >> >>> I was wondering why the following doesn't work: >>> >>>> a=c(1,2) >>>> names(a)=c("one","two") >>>> a >>> one two >>> 1 2 >>>> >>>> names(a[2]) >>> [1] "two" >>>> >>>> names(a[2])="too" >>>> names(a) >>> [1] "one" "two" >>>> a >>> one two >>> 1 2 >>> >>> I must not be understanding some basic concept here. >>> Why doesn't the 2nd name change to "too"? >> >> I cannot tell you why, perhaps you are not actually working with the >> names of a, but I can show you that: >> >>> names(a)[2] <- "too" >>> a[2] >> too >> 2 >>> a >> one too >> 1 2 >> >> And this is seen as well in the help page examples. The help page >> also >> says the following, which I cannot understand: >> >> It is possible to update just part of the names attribute via the >> general rules: see the examples. This works because the expression >> there is evaluated as z <- "names<-"(z, "[<-"(names(z), 3, "c2")). >> > > the following: > > names(a[2]) = 'foo' > > has (partially) a functional flavour, in that you assign to the names > of > a *copy* of a part of a, while > > names(a)[2] = 'foo' > > does not have the flavour, in that you assign to the names of a; it > seems, according to the man page you quote, to be equivalent to: > > a = 'names<-'(a, '[<-.'(names(a), 2, 'foo')) > > which proceeds as follows: > > tmp1 = names(a) > # get a copy of the names of a, no effect on a > > tmp2 = '[<-'(tmp1, 2, 'foo') > # get a copy of tmp1 with the second element replaced with 'foo' > # no effect on either a or tmp1 > > tmp3 = 'names<-'(a, tmp2) > # get a copy of a with its names replaced with tmp2 > # no effect on either a, tmp1, or tmp2 > > a = tmp3 > # backassign the result to a > > vQ > > ______________________________________________ > 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.
Hi Wacek: Somewhere I remember reading that environments have functionality like lists EXCEPT for the names part. IIRC, I think that I read this in the R Language Reference manual also. On Wed, Feb 25, 2009 at 4:32 AM, Wacek Kusnierczyk wrote:> a quick follow-up: > > e = new.env() > e$a = 1 > names(e) > # NULL > names(e) = 'a' > # error in names(e) = "foo" : names() applied to a non-vector > > this is surprising. names(e) 'works', there is no complaint, but when > names<- is used, the error is about the use of names, not names<-. > > btw. ?names says: > > "Description: > > Functions to get or set the names of an object. > > Usage: > > names(x) > names(x) <- value > > Arguments: > > x: an R object. > " > > and there is no clarification in the rest of the page that x cannot be > an environment, or that it has to be a vector. furthermore: > > p = pairlist(a=1) > names(p) > # "a" > names(p) = 'b' > # fine > is.vector(p) > # FALSE > > which is incoherent with the above error message, in that p is *not* a > vector. > > vQ > > > > Wacek Kusnierczyk wrote: >> >> the following: >> >> names(a[2]) = 'foo' >> >> has (partially) a functional flavour, in that you assign to the names >> of >> a *copy* of a part of a, while >> >> names(a)[2] = 'foo' >> >> does not have the flavour, in that you assign to the names of a; it >> seems, according to the man page you quote, to be equivalent to: >> >> a = 'names<-'(a, '[<-.'(names(a), 2, 'foo')) >> >> which proceeds as follows: >> >> tmp1 = names(a) >> # get a copy of the names of a, no effect on a >> >> tmp2 = '[<-'(tmp1, 2, 'foo') >> # get a copy of tmp1 with the second element replaced with 'foo' >> # no effect on either a or tmp1 >> >> tmp3 = 'names<-'(a, tmp2) >> # get a copy of a with its names replaced with tmp2 >> # no effect on either a, tmp1, or tmp2 >> >> a = tmp3 >> # backassign the result to a >> > > ______________________________________________ > 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.
Thanks Berwin. You're correct in that I meant the R Language Definition. Well, it may be a draft but I read it for the first time a few months ago and it was very enlightening so, whether it's a draft or not, I highly recommend it. ( but not for total beginners. The R-intro is better for a more gentle introduction ). On Wed, Feb 25, 2009 at 4:34 AM, Berwin A Turlach wrote:> On Wed, 25 Feb 2009 09:57:36 +0100 > Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote: > >> markleeds at verizon.net wrote: >>> David, Wacek: Just so everyone knows, I just looked and this is >>> explained quite clearly in the R Language Reference manual, very >>> similarly to what Wacek did below. >>> >> thanks, that's good, because i made it up following the page quoted >> by >> david, and if i'm flamed for nonsense, the langref has to be flamed >> too. > > Actually, no. :) > > I do not remember the details, but I once tried to use the "R Language > Definition" (I presume this is what we are talking about) to support > my > argument about some issue I had at that time. It was pointed out to > me > that the "R Language Definition" is a draft, hence not authoritative; > and it seems its status of being a draft has not changed since. > > Cheers, > > Berwin
markleeds at verizon.net wrote:> Hi Wacek: Somewhere I remember reading that environments have > functionality like lists EXCEPT for the names part. IIRC, I think that > I read this in the R Language Reference manual also. > >this would be a confused and confusing statement, unless 'functionality' has some rather vague sense here. (which would not be very surprising.) if you find it, i humbly suggest that you politely report it as a statement to be fixed. there are important differences between environments and lists, in particular wrt. to assignment: l = list(a=1) ll = l l$a = 0 l$a == ll$a # FALSE e = new.env() e$a = 1 ee = e e$a = 0 e$a == ee$a # TRUE this is a fundamental difference, one that could/should be more carefully acknowledged in functions that return objects represented with environments as opposed to lists (e.g., srcfile). vQ