> -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of anna freni > sterrantino > Sent: Thursday, November 06, 2008 10:00 AM > To: cruz; r-help@r-project.org > Subject: Re: [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) > > 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 >
You can also use the column name with the matrix A[,'X'] Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 ______________________________________________ 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.