Hi, Is this intended behaviour of cbind?> a<-c(0,1,2,3) > a[1] 0 1 2 3> a<-as.ordered(a) > a[1] 0 1 2 3 Levels: 0 < 1 < 2 < 3> a<-a[a!=0] #remove the zero from a > a[1] 1 2 3 Levels: 0 < 1 < 2 < 3> cbind(a)a [1,] 2 [2,] 3 [3,] 4 #cbind adds +1 to each element> a<-as.ordered(as.vector(a)) > a[1] 1 2 3 Levels: 1 < 2 < 3> cbind(a)a [1,] 1 [2,] 2 [3,] 3 #now it works... I am running R 2.3.0 on a windows system. Regards, Dirk Vandekerckhove
On Tue, 2006-06-06 at 13:44 -0700, Dirk Vandekerckhove wrote:> Hi, > > Is this intended behaviour of cbind? > > > a<-c(0,1,2,3) > > a > [1] 0 1 2 3 > > a<-as.ordered(a) > > a > [1] 0 1 2 3 > Levels: 0 < 1 < 2 < 3 > > a<-a[a!=0] #remove the zero from a > > a > [1] 1 2 3 > Levels: 0 < 1 < 2 < 3 > > cbind(a) > a > [1,] 2 > [2,] 3 > [3,] 4 > > #cbind adds +1 to each elementDoes this help?> a[1] 1 2 3 Levels: 0 < 1 < 2 < 3> as.integer(a)[1] 2 3 4 Note in ?cbind, the Details section indicates: "In the default method, all the vectors/matrices must be atomic (see vector) or lists (e.g., not expressions)." For a factor, the atomic data type is the underlying integer vector. You eliminated '0' from the original ordered factor, which had an integer value of 1 (not 0!):> a[1] 0 1 2 3 Levels: 0 < 1 < 2 < 3> as.integer(a)[1] 1 2 3 4 Unless you re-level the factor (as you do below) the other elements retain the original integer values.> > a<-as.ordered(as.vector(a)) > > a > [1] 1 2 3 > Levels: 1 < 2 < 3 > > cbind(a) > a > [1,] 1 > [2,] 2 > [3,] 3 > > #now it works...Yep, you re-leveled 'a', so the integer values now correspond to the levels:> a<-as.ordered(as.vector(a))> a[1] 1 2 3 Levels: 1 < 2 < 3> as.integer(a)[1] 1 2 3 HTH, Marc Schwartz
Hi, It doesn't have anything to with cbind, but rather with as.ordered - you converted it to a factor in that step. In the cbind step, you are actually getting the position of that ordered factor, rather than anything to do with the values themselves.> a <- c(1,7,5,3) > a <- as.ordered(a) > a[1] 1 7 5 3 Levels: 1 < 3 < 5 < 7> cbind(a)a [1,] 1 [2,] 4 [3,] 3 [4,] 2 The original values could be letters, or anything, and you'd still get the same result with the same ordering. Sarah On 6/6/06, Dirk Vandekerckhove <dvdkerc@yahoo.com> wrote:> > Hi, > > Is this intended behaviour of cbind? > > > a<-c(0,1,2,3) > > a > [1] 0 1 2 3 > > a<-as.ordered(a) > > a > [1] 0 1 2 3 > Levels: 0 < 1 < 2 < 3 > > a<-a[a!=0] #remove the zero from a > > a > [1] 1 2 3 > Levels: 0 < 1 < 2 < 3 > > cbind(a) > a > [1,] 2 > [2,] 3 > [3,] 4 > > #cbind adds +1 to each element > > > a<-as.ordered(as.vector(a)) > > a > [1] 1 2 3 > Levels: 1 < 2 < 3 > > cbind(a) > a > [1,] 1 > [2,] 2 > [3,] 3 > > #now it works... > > I am running R 2.3.0 on a windows system. > > > > Regards, > > Dirk Vandekerckhove > > ______________________________________________ > R-help@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 >-- Sarah Goslee [[alternative HTML version deleted]]