hi, when I used cbind to combine columns, some contents of columns has been replaced by number. in the script below, column should be aaa,bbb,ccc but I was given 1,2,3. but when I change the column to vector, it gave me correct contents. can you please tell me why?> d<-read.table("aaa.txt") > dV1 V2 1 aaa 123 2 bbb 345 3 ccc 986> cbind(d[,1],d[,2])[,1] [,2] [1,] 1 123 [2,] 2 345 [3,] 3 986> cbind(as.vector(d[,1]),d[,2])[,1] [,2] [1,] "aaa" "123" [2,] "bbb" "345" [3,] "ccc" "986">[[alternative HTML version deleted]]
Le Sun, Aug 24, 2008 at 10:56:43PM -0700, Yuan Jian a ?crit :> hi, > when I used cbind to combine columns, some contents of columns has been replaced by > number. in the script below, column should be aaa,bbb,ccc but I was given 1,2,3. > but when I change the column to vector, it gave me correct contents. can you please > tell me why? > ? > > d<-read.table("aaa.txt")Hello, read.table converts characters to factors. Factors will be converted to numbers by cbind, as in the following example:> toto <- c("aaa","bbb","aaa") > toto[1] "aaa" "bbb" "aaa"> as.factor(toto)[1] aaa bbb aaa Levels: aaa bbb> cbind(as.factor(toto))[,1] [1,] 1 [2,] 2 [3,] 1 The 'as.is' or 'colClasses' option of read.table will help you to prevent the conversion of characters to factors. Have a nice day, -- Charles Plessy http://charles.plessy.org Tsurumi, Kanagawa, Japan