[R] Newbie "if" problem

Erik Iverson iverson at biostat.wisc.edu
Thu Dec 18 02:02:09 CET 2008



glenn roberts wrote:
> I have got a general problem when applying a function to a dataframe using
> the function;
> 
> Apply(df,1,myfunct)


Do you mean "apply"?  Or does the "Aaply" function come from a separate 
package?

> 
> Where myfunct has an IF statement in it along the lines of;
> 
> If (z == 0)
> X = 1
> If (z ==0)
> Z = 1
> 
> I.e. Two If statements
> 
> Is there something I am missing or have a just formed the if statements
> wrong ­ just checking there is not some trick you have to use when using
> apply with functions with if statements in.
> 
> The function works fine in isolation with df[1,.] say

You are forgetting to include details here, specifically, a reproducible 
example of a function called 'myfunct' that exhibits the behavior you 
aren't expecting.

'myfunct' is going to have to take a parameter (say, 'x') that contains 
the data.  If you call it 'x', you might try replacing z above by x["z"] 
perhaps?  It's difficult to say more until we have more details.



More information about the R-help mailing list