From p.dalgaard@pubhealth.ku.dk  Fri Mar  2 17:40:17 2001
From: p.dalgaard@pubhealth.ku.dk (Peter Dalgaard BSA)
Date: 02 Mar 2001 18:40:17 +0100
Subject: [Rd] apply on data.frame does not work properly (PR#859)
In-Reply-To: sergei@stams.strath.ac.uk's message of "Fri, 2 Mar 2001 18:14:31 +0100 (MET)"
References: <200103021714.SAA10405@pubhealth.ku.dk>
Message-ID: <x2snkw83y6.fsf@blueberry.kubism.ku.dk>

sergei@stams.strath.ac.uk writes:

> Full_Name: Dr. Sergei Zuyev
> Version: 1.1.0 and 1.2.2
> OS: Solaris
> Submission from: (NULL) (130.159.248.36)
> 
> 
> apply on data.frame with floats, factors and NA's does not work properly. Here
> is
> an example:
> I am searching for rows containig NA values.
> 
> X1 <- c(1, NA)
> X2<-c("A","B")
> df<-data.frame(X1,X2)
> apply(df,1,function(x){any(is.na(x))})
> # gives
> # 1     2
> # FALSE TRUE
> # as it should
>  
> # But:
> X1 <- c(1.5, NA) # float instead of natural
> df<-data.frame(X1,X2)
> apply(df,1,function(x){any(is.na(x))})
> # gives
> # 1     2
> # FALSE FALSE
> # as it shouldn't
>  
> # However,
> apply(df[2,],1,function(x){any(is.na(x))})
> # gives TRUE
>  
> # Change X2 to a numeric vector and everything works fine:
> X2<-c(2.5,3)
> df<-data.frame(X1,X2)
> apply(df,1,function(x){any(is.na(x))})
> # gives
> # 1     2
> # FALSE TRUE
> # as it shouldn't                                                          
 
This traces back to the use of format() when coercing dataframes to
character:

> format(c(1,NA))
[1] " 1" "NA"
> format(c(1000,NA))
[1] "1000" "  NA"

And "  NA" is not a character NA....

apply(is.na(df),1,any) seems to be better though.


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] apply on data.frame does not work properly (PR#859)
Peter Dalgaard BSA