Re: [R] How to avoid "$ operator is invalid for atomic vectors"

From: anna freni sterrantino <annafreni_at_yahoo.it>
Date: Thu, 06 Nov 2008 17:59:47 +0000 (GMT)

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 <cruadam_at_gmail.com>
A: r-help_at_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_at_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_at_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. Received on Thu 06 Nov 2008 - 18:02:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Nov 2008 - 18:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive