Skip to content
Prev 280968 / 398503 Next

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

Dear Alex,
You'll probably get a better answer if you don't keep what you want to do a
secret.
Yes. As I said, a data frame is a list of columns, so FUN is called with
each column as its argument.
I'm afraid that I don't know what you mean. Do you want to deal with the
columns of the data frame separately (in general, they need not all be of
the same class), and within each column, apply a function separately to each
element? You could nest calls to lapply() or sapply(), as in

sapply(D, function(DD) sapply(DD, abs))

assuming, of course, that D is an entirely numeric data frame. But in this
case,

abs(as.matrix(D))

would be more sensible, and using sapply() like this isn't necessarily
better than a loop. Again, not knowing what you want to do makes it hard to
suggest a solution.

Best,
 John