FAQ/Ratomic - CBU statistics Wiki

Revision 1 as of 2009-06-10 12:18:31

Clear message
location: FAQ / Ratomic

An Error caused by using an inappropriate data type in R - and its solution

Using some versions of R (2.8 and 2.9) you sometimes get the mystifying error message "$ operator is invalid for atomic vectors" . This can happen even if the R code you are running worked in earlier versions.

This may be due to inputting data as matrix data rather than a dataframe in R. You can so this conversion easily using the command as.data.frame.

% matrix data
b=matrix(a,3,2,byrow=T)
% converted to dataframe format
c=as.data.frame(b)

This advice is taken from [https://stat.ethz.ch/pipermail/r-help/2008-November/179050.html here] and reproduced in full below:

>


Original Message


> From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of anna freni > sterrantino > Sent: Thursday, November 06, 2008 10:00 AM > To: cruz; r-help at 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