Dear all,

Happy new year!

I have a question re using sapply. Below is a dummy example that would 
replicate the error I saw. 

##Code Starts here
DummyFunc <- function(x) {

if (x > 0) {
return (x)
} else
{
return (-x)
}

}

Y = data.frame(val = c(-3:7))
sapply(Y, FUN = DummyFunc)
##Code ends here

When I run it, I got:
     val
 [1,]   3
 [2,]   2
 [3,]   1
 [4,]   0
 [5,]  -1
 [6,]  -2
 [7,]  -3
 [8,]  -4
 [9,]  -5
[10,]  -6
[11,]  -7
Warning message:
In if (x > 0) { :
  the condition has length > 1 and only the first element will be used

The result is different from what I would expect plus there is such an error 
message.

I guess if the DummyFunc I provided is compatible with vectors, the problem 
would go away. But let's suppose I cannot change DummyFunc. Is there still a 
way to use sapply or alike without actually writing a loop? Thanks.

- Alex
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to