[R] Taking mean of 3 cols in data frame : error

David Winsemius dwinsemius at comcast.net
Sat Jan 30 07:37:37 CET 2010


On Jan 30, 2010, at 12:46 AM, Abhishek Pratap wrote:

> I exactly know my problem. The read.table by default reads my data
> from few columns as factors/character type.  Can you please give me an
> example of how to force it to read these columns as numbers. I dont
> see example of how to use (as.is)

You can use colClasses() or set stringsAsFactors=FALSE.

>
> Thanks!
> -Abhi
>
> On Sat, Jan 30, 2010 at 12:34 AM, Abhishek Pratap
> <abhishek.vit at gmail.com> wrote:
>> I see what is happening here. When I am reading the data frame values
>> from a csv file, they are not read as numerics.
>>
>> _A
>>
>> On Sat, Jan 30, 2010 at 12:29 AM, Abhishek Pratap
>> <abhishek.vit at gmail.com> wrote:
>>> Hi All
>>>
>>> I am seeing quite a silly thing for which I dont have much of
>>> explanation. I want to take per row mean of 3 columns of a data  
>>> frame.
>>>  What I am getting is  all NA in the result. Here is what I am doing
>>>
>>>
>>> apply(a[,1:3],1,mean)
>>>
>>> I get warnings.
>>>
>>>  In mean.default(newX[, i], ...) :
>>>  argument is not numeric or logical: returning NA
>>>
>>> I am not sure how it thinks the values are not numeric. May be I  
>>> had a
>>> long day and not able get something fairly obvious.
>>>
>>> ==========
>>> summary(a)
>>>
>>>  animal1_wt             animal2_wt                               
>>> animal3_wt
>>>  0         : 5448          0         : 5295                
>>> 0         :
>>> 5501
>>>  0.232266  :    7          0.17356   :    5            
>>> 6.22424   :    6
>>>  1.05487   :    6          0.00630758:    4            
>>> 0.0395818 :    5
>>>  0.258336  :    5          0.0131442 :    4            
>>> 4.14949   :    5
>>>  0.372308  :    5          0.0143924 :    4            
>>> 0.00964998:    4
>>>  0.00926343:    4          0.017208  :    4            
>>> 0.0172297 :    4
>>>  (Other)   :15696          (Other)   :15855           (Other)   : 
>>> 15646
>>>
>>
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list