# [R] problem adding columns to matrix

Daniel Nordlund djnordlund at verizon.net
Mon Aug 10 01:51:15 CEST 2009

```> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Fabio Murtas
> Sent: Sunday, August 09, 2009 1:51 PM
> To: R-help at r-project.org
> Subject: [R] problem adding columns to matrix
>
> Hi all, i purchased a copy of the book Morphometrics with R
> by Springer.
> at the end of each chapter there are exercises to train what
> you just
> read and (hope) learned...
>
> so i have this problem:
>
> Define a hypothetical data frame containing five measurments
> normally
> distributed(size,head,pectoral,area,weight) for four individuals
> (named ind1, ind2, etc). ADD A COLUMN corresponding to the
> factor sex
> with individual 1 & 2 being Males and 3 & \$ being Females.
>
>
>
> HERE IS HOW I PROCEDED, getting 2 problems
> 1) what are those warning messages i got when i generate the
> m matrix
> (the italian text stand for "the numerical expression has 5
> elements:
> just the first is used")
> 2) why when i use cbind to add the s factor wit M,M,F,F,
> (sexes) to
> the m matrix i get numerical values 1 & 2 instead of
> character values
> M & F?
>
> --------------------------------------------------------------
> ----------------------------------------
> procedure follows:
>
>  > a<- rnorm(5, 20, 7)
>  > a
> [1] 27.32586 34.07330 21.11710 25.85710 22.68296
>  > a
> [1] 27.32586 34.07330 21.11710 25.85710 22.68296
>  > b<- rnorm(5, 20, 7)
>  >
>  > d<- rnorm(5, 20, 7)
>  >
>  >
>  > e<- rnorm(5, 20, 7)
>
>  > m<-matrix(a:e, 4,5)
> Warning messages:
> 1: In a:e :
>   l'espressione numerica ha 5 elementi: solo il primo è utilizzato
> 2: In a:e :
>   l'espressione numerica ha 5 elementi: solo il primo è utilizzato
> 3: In matrix(a:e, 4, 5) :
>   data length [3] is not a sub-multiple or multiple of the number of
> rows [4]
>  >
>  > m
>          [,1]     [,2]     [,3]     [,4]     [,5]
> [1,] 27.32586 28.32586 29.32586 27.32586 28.32586
> [2,] 28.32586 29.32586 27.32586 28.32586 29.32586
> [3,] 29.32586 27.32586 28.32586 29.32586 27.32586
> [4,] 27.32586 28.32586 29.32586 27.32586 28.32586
>
>  > m<-as.data.frame(m)
>  > m
>         V1       V2       V3       V4       V5
> 1 27.32586 28.32586 29.32586 27.32586 28.32586
> 2 28.32586 29.32586 27.32586 28.32586 29.32586
> 3 29.32586 27.32586 28.32586 29.32586 27.32586
> 4 27.32586 28.32586 29.32586 27.32586 28.32586
>  > mode(m)
> [1] "list"
>  > colnames(m)<-c("size","head","pectoral","area","weight")
>  > mode(m)
> [1] "list"
>  >
>  > m
>       size     head pectoral     area   weight
> 1 27.32586 28.32586 29.32586 27.32586 28.32586
> 2 28.32586 29.32586 27.32586 28.32586 29.32586
> 3 29.32586 27.32586 28.32586 29.32586 27.32586
> 4 27.32586 28.32586 29.32586 27.32586 28.32586
>
>  >rownames(m)<-paste("ind", 1:4, sep="")
>  >
>  >
>  > m
>          size     head pectoral     area   weight
> ind1 27.32586 28.32586 29.32586 27.32586 28.32586
> ind2 28.32586 29.32586 27.32586 28.32586 29.32586
> ind3 29.32586 27.32586 28.32586 29.32586 27.32586
> ind4 27.32586 28.32586 29.32586 27.32586 28.32586
>
>  > s<-factor(c("m","m","f","f"))
>  >
>  >
>  > s
> [1] m m f f
> Levels: f m
>  > cbind(m,s)
>          size     head pectoral     area   weight s
> ind1 27.32586 28.32586 29.32586 27.32586 28.32586 2
> ind2 28.32586 29.32586 27.32586 28.32586 29.32586 2
> ind3 29.32586 27.32586 28.32586 29.32586 27.32586 1
> ind4 27.32586 28.32586 29.32586 27.32586 28.32586 1
>  >
> --------------------------------------------------------------
> ----------------------------------------
>
> Thanks in advance
>

Fabio,

For question 1, the warning message is telling you that

m<-matrix(a:e, 4,5)

Is not doing what you think it is.  The syntax a:e creates a sequence, but
it is expecting a to be a single number, not a vector.  So, it is telling
you that it is only going to use the first value from  vector a.  Likewise,
it will only use the first value of vector e for the end value.  So
effectively, you statement gets interpreted as

m<-matrix(a[1]:e[1], 4,5)

Based on the random numbers you generated this created a sequence of
length=3.  You matrix was then created using just those 3 numbers (the first
3 numbers in the first column of your matrix) and recycled them as needed to
fill out the matrix.

You probably should have create the data frame directly, something like

df <- data.frame(rbind(a,b,d,e))

As for question 2, I don't know why you got the values 1,2 in your data
frame.  When I ran your R code, the data frame I ended up with had the
values 'm', 'f'.  The values 1,2 are the underlying numeric values of the
factror you created, but why they showed up, I don't know.  It could have to
do with different versions of R, and or operating systems, etc., which you
told us nothing about.  I am using R-2.9.1 on Win XP Pro.

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

```

More information about the R-help mailing list