Skip to content
Prev 280959 / 398503 Next

sapply Call Returning " the condition has length > 1" Error

Dear Alex,
This is a warning, not really an error message. A data frame is essentially
a list of variables (columns), and sapply() applies its FUN argument to each
list element, that is, each variable -- the one variable val in your case.
That produces a warning because val > 0 is a vector of 11 elements, and the
first comparison, 3 > 0, which is TRUE, controls the result.
Well, you could just use
[1] 3 2 1 0 1 2 3 4 5 6 7

but I suppose that you didn't really want to write your own version of the
absolute-value function as something more than an exercise.

An alternative is
[1] 3 2 1 0 1 2 3 4 5 6 7

I hope this helps,
 John

--------------------------------
John Fox
Senator William McMaster
  Professor of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox