[R] problem with "APPLY"
De France Henri
henri_france at yahoo.fr
Wed May 20 16:45:25 CEST 2009
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 ?
Thanks,
Henri
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: data.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090520/6798b6f1/attachment-0002.txt>
More information about the R-help
mailing list