[R] problem with "APPLY"

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed May 20 17:16:09 CEST 2009


De France Henri wrote:
> Hello,
>  
> The "apply" function seems to behave oddly with my code below
>  
> NB : H1 is a data frame. (data in the attached file.)
> # the first lines are:
> 1 02/01/2008 0.000000  0  0 0.000000   0
> 2 03/01/2008 0.000000  0  0 0.000000   0
> 3 04/01/2008 0.000000  0  0 0.000000   0
> 4 07/01/2008 0.000000  0  0 0.000000   0
> 5 08/01/2008 0.000000  0  0 0.000000   0
> 6 09/01/2008 0.000000  0  0 0.000000   0
> 7 10/01/2008 0.000000  0  0 0.000000   0
> 8 11/01/2008 1.010391  0  0 1.102169   0
> ...
> The aim of the code is to extract those lines for which there is a strictly positive value in the second column AND in one of the others:
>  
> reper=function(x){as.numeric(x[2]>1 & any(x[3:length(x)]>1))}
>  
> TAB1= H1[which(apply(H1,1,reper)>0),]
>  
> Strangely, this is OK for all the lines, except for the last one. In fact, in H1, the last 2 lines are:
> 258 29/12/2008 1.476535 1.187615  0 0.000000   0
> 259 30/12/2008 0.000000 1.147888  0 0.000000   0
> Obviously, line 258 should be the last line of TAB1, but it is not the case (it does not appear at all) and I really don't understand why. This is all the more strange since applying the function "reper" only to this line 258 gives a "1" as expected...
> Can someone help ?
>  

Works for me...

        do...1.       V3       V5 V7      V13 V31
213 24/10/2008 2.038218 2.820196  0 0.000000   0
214 27/10/2008 3.356057 2.588509  0 2.101651   0
219 03/11/2008 2.122751 1.648410  0 2.180908   0
233 21/11/2008 1.439861 1.883605  0 1.359372   0
234 24/11/2008 1.216548 1.480797  0 1.049390   0
258 29/12/2008 1.476535 1.187615  0 0.000000   0

You are crossing the creek to fetch water, though:

reper <- function(x) x[2]>1 & any(x[3:length(x)]>1)
TAB1 <-  H1[apply(H1,1,reper),]

or even

TAB1 <-  H1[ H1[2] > 1  & apply(H1[3:6] > 1, 1, any),]


-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list