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") > AX Y [1,] NA NA [2,] NA NA [3,] NA NA> A$XError in A$X : $ operator is invalid for atomic vectors>Thanks, cruz
Erik Iverson
2008-Nov-06 16:36 UTC
[R] How to avoid "$ operator is invalid for atomic vectors"
Hello - cruz wrote:> 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 vectorsA[, "X"] may be what you want? See the details of ?Extract about how '$' only works on recursive (list-like) objects, of which your matrix A is not one of.> > 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.
anna freni sterrantino
2008-Nov-06 17:59 UTC
[R] How to avoid "$ operator is invalid for atomic vectors"
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) > cx 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@gmail.com> 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") > AX Y [1,] NA NA [2,] NA NA [3,] NA NA> A$XError 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]]