# [R] problem adding columns to matrix

Fabio Murtas fabio.murtas at gmail.com
Sun Aug 9 22:51:17 CEST 2009

```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

so i have this problem:

Define a hypothetical data frame containing five measurments normally
(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"
> mode(m)
[1] "list"
>
> m
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
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
>
------------------------------------------------------------------------------------------------------