[R] Re :argument is not numeric or logical

David Winsemius dwinsemius at comcast.net
Sun May 2 17:52:58 CEST 2010


On May 2, 2010, at 11:22 AM, Mohan L wrote:

>
>
>
>
> Your code has a different name for the sample object. And it would  
> be more informative if you offered str on "sample1".
>
> This is the result of str on "sample1".
> > str(sample1)
> 'data.frame':    35943 obs. of  17 variables:
>  $ stdate     : Factor w/ 7 levels "01/11/09 00:00",..: 1 1 1 1 1 1  
> 1 1 1 1 ...
>
> So stdate is not a date variable but that is probably not your  
> problem since it looks like the time portion of your not-dates are  
> all "00:00". tapply can work with this information
>
>
> snipped
>
>
>  $ Login      : Factor w/ 419 levels ".00","1.00","10.00",..: 114  
> 283 217 216 14 1 2 2 407 327 ...
>
> So "Login" is not a numeric class variable. Read the FAQ about the  
> proper way to convert that variable to numeric without loosing  
> information.
>
> The Login column contain all numeric values . I don't understand why  
> I need to convert that variable to numeric?

BECAUSE IT"S NOT NUMERIC.

I have no way to determining  _why_ it is not numeric, but it simply  
is ... not. Your input method turned it into a factor. Perhaps there  
was one missing delimiter, or there was a stray character in one of  
the entries in the file. Who knows. Why waste time arguing? Follow the  
directions for fixing the problem. Once you do so, location of the  
element causing the problem may become more clear, since then the non- 
numeric coercible elements will get turned into "NA"'s or "NaN"'s.


>
> login column contain data like this :
>
> > sample$Login
>
> [35881] .00      1.00     .00      1.00     .00      .00       
> 1.00     2.00
> [35889] 1.00     3.00     .00      .00      .00      1.00     . 
> 00      32.00
> [35897] 2.00     4.00     .00      17.00    3.00     1.00      
> 12.00    .00
> [35905] 8.00     .00      3.00     7.00     .00      17.00    . 
> 00      4.00
> [35913] 16.00    4.00     20.00    7.00     .00      22.00     
> 18.00    1.00
> [35921] 3.00     3.00     .00      .00      37.00    3.00      
> 12.00    3.00
> [35929] 2.00     11.00    .00      5.00     77.00    1.00      
> 4.00     1.00
> [35937] 9.00     18.00    3.00     3.00     4.00     14.00    2.00

Factor variables can have labels that look like numbers.

>
> The mode of Login is numeric only :

So what?

 > mode(factor(c("a", "v", "c"))  )
[1] "numeric"

Modes are not the same as classes. Factors are represented internally  
as integers.


>
> > mode(sample$Login)
> [1] "numeric"
> >
>
>  And I am also using as.numeric(as.character(sample$Login), still it  
> has some warning and NA . I don't understand why this is happening.   
> could you explain?

I already did explain. You are bordering on magical thinking here.  
Since the class of sample$Login is "factor", your fervent desire to  
make it "numeric" will accomplish nothing useful.

-- 
David.
>
> > avglog <- with(sample1, tapply(as.numeric(as.character(sample 
> $Login)), stdate, mean))
> Warning message:
> In tapply(as.numeric(as.character(sample$Login)), stdate, mean) :
>   NAs introduced by coercion
> > avglog
> 01/11/09 00:00 02/11/09 00:00 03/11/09 00:00 04/11/09 00:00 05/11/09  
> 00:00
>       22.04866       16.65358             NA       15.84970        
> 16.58600
> 06/11/09 00:00 07/11/09 00:00
>       16.20743       18.52379
>
> Thanks & Rg
> Mohan L
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list