[R] Problem with col

John Kane jrkrideau at inbox.com
Mon Apr 20 19:38:24 CEST 2015




> -----Original Message-----
> From: soniaamin5 at gmail.com
> Sent: Mon, 20 Apr 2015 18:56:19 +0200
> To: sarah.goslee at gmail.com
> Subject: Re: [R] Problem with col

 --- clip--
> 
> When I type data , I obtain all the numeric values and the headears  I
> added (Consommation,Cylindre,Puissance,Poids)

No you probably do not, as Sarah explained.

As a quick example of the issue look at the two data sets below. Just copy and paste into your R editor.  Both data sets are in dput() format which is how you should supply sample data to R-help.

ddat1  <-   structure(list(aa = structure(1:4, .Label = c("a", "b", "c",
"d"), class = "factor"), bb = 1:4), .Names = c("aa", "bb"), row.names = c(NA,
-4L), class = "data.frame")

ddat2 <- structure(list(aa = c("a", "b", "c", "d"), bb = c("1", "2", "3", "4")), .Names = c("aa", "bb"), row.names = c(NA, -4L), class = "data.frame")

If yo do
dat1
dat2
they look the same on the screen but if you do str()  they are not the same.
str(dat1) 
str(dat2)

Also try 
ddat1$bb * 5  #works
ddat2$bb * 5 # error!


They look the same on the computer screen but they are quite different.

John Kane
Kingston ON Canada



> 
> Thanks
> 
> 
> 
> 2015-04-20 18:40 GMT+02:00 Sarah Goslee <sarah.goslee at gmail.com>:
> 
>> What is the problem? One or more of your columns was read as factor, as
>> 
>> str(data)
>> 
>> would show you. To avoid this, you can add stringsAsFactors=FALSE to
>> the read.table command, but if you expect your data to be entirely
>> numeric then there's something wrong with it that you need to hunt
>> down.
>> 
>> Sarah
>> 
>> On Mon, Apr 20, 2015 at 12:33 PM, Sonia Amin <soniaamin5 at gmail.com>
>> wrote:
>>> Dear All,
>>> 
>>> I have written the following lines:
>>> 
>>> 
>> data<-read.table("C:\\Users\\intel\\Documents\\SIIID\\datamultiplereg.txt",header
>>> = FALSE, sep = "")
>>>  colnames(data)<-c("Consommation","Cylindre","Puissance","Poids")
>>>  result.model1<-lm(Consommation~Cylindre+Puissance+Poids, data=data)
>>> summary(result.model1)
>>> 
>>> I obtained the following message:
>>> 
>>> 
>>> Call:
>>> lm(formula = Consommation ~ Cylindre + Puissance + Poids, data = data)
>>> 
>>> Residuals:
>>> Error in quantile.default(resid) : factors are not allowed
>>> In addition: warning message:
>>> In Ops.factor(r, 2) :
>>>   ‘^’ This is not relevant for factors
>>> 
>>> 
>>> Where is the problem?
>>> Thank you in advance
>>> 
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

____________________________________________________________
Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords & protects your account.



More information about the R-help mailing list