Hi Cruz
you don't need to assign dimension or classes to your objects.
It's easier if you  do like this


> a=c(0,1,2,4,1,1)
> length(a)
[1] 6
> b=matrix(a,3,2,byrow=T)
> b
     [,1] [,2]
[1,]    0    1
[2,]    2    4
[3,]    1    1
of course you can change the colnames and assign what 
you prefer

> colnames(b)=c("x","y")

but if you try to recall "x" with
b$x 
is not going to work
like that, 
you have two option:

1. switch  form matrix to a dataframe:
> c=as.data.frame(b)
> c
  x y
1 0 1
2 2 4
3 1 1
> c$x
[1] 0 2 1

no problems.

2. Can get the column  "x"
on the matrix b as
b[,1]
[1] 0 2 1

just giving the  position.


Hope that this helps.

Best Regards
Anna



 Anna Freni Sterrantino
Ph.D Student 
Department of Statistics
University of Bologna, Italy
via Belle Arti 41, 40124 BO.




________________________________
Da: cruz <[EMAIL PROTECTED]>
A: r-help@r-project.org
Inviato: Giovedì 6 novembre 2008, 17:22:42
Oggetto: [R] How to avoid "$ operator is invalid for atomic vectors"

Hi,

I am writing this in a wrong way, can someone please correct me?

> A <- matrix()
> length(A) <- 6
> dim(A) <- c(3,2)
> colnames(A) <- c("X","Y")
> A
      X  Y
[1,] NA NA
[2,] NA NA
[3,] NA NA
> A$X
Error in A$X : $ operator is invalid for atomic vectors
>

Thanks,
cruz

______________________________________________
R-help@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.



      Unisciti alla community di Io fotografo e video, il nuovo corso di 
fotografia di Gazzetta dello sport:

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to